We discuss a useful tool – Query Monitor – that helps with debugging slow WordPress database queries.
Why Query Optimization?
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.
|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.
|Request||Tracks hooks related to rewrite rules, query parsing, request handling, query vars, |
|Blocks||Gives the Block types we registered in the post.|
|Template||Shows the template filename, the complete template hierarchy, and the names |
of all template parts that were loaded.
Enqueued scripts along with their dependencies, and also alerts for broken
Enqueued stylesheets along with their dependencies.
|Hooks and Actions||Hooks and actions from the WP core and the WP plugins that are used |
in the post rendering.
|HTTP API Calls||HTTP API requests with the associated response codes component making |
the request, time taken and alerts for failed or erroneous requests.
|Transient Updates||Provides 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.