We discuss a useful tool – Query Monitor – that helps with debugging slow WordPress database queries. 

Why Query Optimization?

WordPress is usually very efficient at executing its internal database queries. So generally speaking, you shouldn’t have to work on optimizing them. Most of the time when a page is slow, the culprits lie elsewhere. You can check our article on using Google’s Tools to debug performance issues. The typical contributors to poor performance are large images, render blocking Javascript, not lazy loading images, not minifying Javascript and such. For some practical guidelines on optimizing images, you can check our guidelines

We recommend investigating WP Query optimization only if you feel you have optimized all of the above items as much as you can. Another good reason to look into it is if your site has a lot of dynamic data and you have created a lot of custom post types to support it. For some useful pointers on how to do dynamic data design in WP, you can check out article

Query Monitor Features

Alright, with all those caveats, let’s jump in. A great place to start is the Query Monitor tool. You can download it here. The plugin lets you analyze much more than DB query times.

Query Monitor
FeaturesDescription
Page Generation Time
Total time to generate a page.
Peak PHP memory usage
Peak memory usage when all of the PHP code, scripts and database queries run.
RequestTracks hooks related to rewrite rules, query parsing, request handling, query vars, 
and more.
BlocksGives the Block types we registered in the post.
TemplateShows the template filename, the complete template hierarchy, and the names 
of all template parts that were loaded.
Scripts
Enqueued scripts  along with their dependencies, and also alerts for broken 
dependencies.
Styles
Enqueued stylesheets along with their dependencies.
Hooks and ActionsHooks and actions from the WP core and the WP plugins that are used 
in the post rendering.
LanguagesList of Javascript translation files which uses wp-i18n as a dependency.
HTTP API CallsHTTP API requests with the associated response codes component making 
the request, time taken and alerts for failed or erroneous requests.
Transient UpdatesProvides a listing of components that use time-limited caching. 

Database Query Optimization

A great place to start is the Query Tab. 

shows all database queries executed along with the corresponding execution time. We can filter queries by query type (SELECT, UPDATE, DELETE) as well as the calling components.  Each query is monitored, timed, and tied back to the function that generated it. As a result, you can sort queries based on how long they take to process and pinpoint problematic, slow-loading queries.

Another useful feature is that you can check for any redundant queries in the Duplicate Queries tab: 

Filtering queries by component or calling functions makes it easy to see which plugins, themes, or functions on your site are making the most (or the slowest) database queries.

Finally, you should also check out the Transient Updates tab for any ineffective caching. Transients are a way of caching data for a predefined time in WordPress. Unlike storing data in the object cache, Transient API allows developers to store information in your WordPress database with an expiration time. 

The Transient Updates section shows any transients that were set, along with their timeout, component, and call stack. In summary, Query Monitor is a wonderful tool to optimize WordPress database queries. While there are more advanced Application Performance Management (APM) products such as New Relic and Firebase, we feel this is a great place to start.

Get in Touch

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Start A Quote

Let us know what you are looking to build and we will be in touch right away.
Fields marked with an * are required
What is your preferred CMS Platform?