WordPress’s Query Monitor: slow and duplicate queries?

John Blackbourn’s Query Monitor plugin sometimes reports slow queries and duplicate queries. How can you interpret these reports? What are slow and duplicate queries?

Slow queries

Query Monitor defines slow queries as those that take more than 50 milliseconds (1/20th of a second). Query Monitor shows query times in seconds in the rightmost column of its display. So a query that takes 55 milliseconds shows up as taking 0.055 seconds. Sometimes, especially on sites with many posts, pages, and/or users, ordinary queries that serve our audiences, like “show the user the first 50 posts and count the total number of posts” take a long time. That’s doubly true on budget hosting providers where many sites share the same database management system. And sometimes there’s something in a plugin that’s slow.

A slow query isn’t necessarily bad. If you have fifty thousand registered users, it is hard to argue with your success even if it takes time to show their names. Still, the core performance team and plugin developers pay attention to slow queries and try to mitigate them.

Query Monitor identifies individual slow queries. But, some pages, especially archive pages, can take a hundred or more separate queries for our servers to deliver.

Duplicate queries

Duplicate queries are just that. Sometimes some code in WordPress or a plugin asks the database for exactly the same information twice during a page view. Fast duplicate queries don’t matter very much. Slow duplicate queries should tell the developer to review their logic so they store the query result in RAM and avoid asking the DBMS to go get it again. The WP_Cache APIs are a formal way of doing that.

Improviing the situation

If you have a big site and slow queries, I suggest you try my Index WP MySQL For Speed plugin. It optimizes the database indexes for some tables. It can speed up many common database queries.

You can reduce the workload on your database by using a persistent object cache plugin. This is an entirely different kind of cache than the typical page cache. When you have a persistent object cache, you’ll still have some slow database queries, but they won’t need to run nearly as often.

If, after that, you still have slow queries you are the victim of success! Your site is big enough and busy enough that you need to upgrade your hosting service. Ask your vendor for “higher capacity on the MariaDB or MySQL database server”. If they laugh or say “huh?” you may need to move to a better vendor.

With the good vendors, we get what we pay for, and high capacity websites take big servers using electricity, and take good networking, both in the data center and out to our users. That stuff ain’t free.

Leave a Comment