Optimize WordPress Database Queries with Query Monitor

Exemplifi
3 min readNov 18, 2020

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.

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.

This article was first published at www.exemplifi.io

--

--