How to Find Slow SQL Queries in a WordPress Blog

By Joe Shockey, Software Engineer

Published: May 27, 2022

Why is my website so slow?

Many things can slow down your WordPress website, large images or video files, scripts, animations, server load and database queries. The good news is that these things are easy to diagnose and remedy. Database queries, however, can be more difficult.

for a quickstart, jump to Query Monitor below

What are Queries?

Most modern websites use a stack of applications, including the webserver’s operating system, a web server, database, and a content management system. Before a web server can send your browser a web page, it sometimes needs to assemble it. Often this means asking a database for specific content. Or, in nerdspeak, it runs a query.

Rule Out the Obvious

To isolate the problem, you need to be able to repeat the problem — when and where is it happening? Is it only specific pages that are slow? Is it only when you are logged in to the backend? Is it only at certain times of the day? Are you using a CDN or web app firewall like Cloudflare or Securi? All of these can be important clues for narrowing down the scope of the problem. Identify a page (or the page) that best illustrates the problem.

Start with Dev tools to rule out the common culprits. Open the network panel, refresh and look at the page load speeds. You can use the waterfall view to see when things are loading and how long they are taking. Look for large assets or slow scripts. Look at the console tab and make sure JavaScript isn’t giving you any errors or getting stuck in a loop. Additionally, you can use Query Monitor (see below) to rule out PHP errors or slow API responses.

Sort by size to find the files taking the longest to load.
Envano.com – Sort by size to find the files taking the longest to load.

Once you have ruled out the obvious, it is time to consider queries.

Is it a query?

Queries coming from WordPress’ core functions are usually optimized and won’t be causing problems. The likely culprits will be obsolete code, 3rd- party plugins or custom code. It’s a good idea to update WordPress, the plugins, and the theme if possible. At the very least, you should be running critical updates to fix security issues.

At this point, you may have a hunch about where the problem lies. It might be only happening on pages using a specific plugin or piece of custom code. Temporarily disabling the plugin or code will confirm your suspicions.

Okay, let’s take a look at the queries!

Query Monitor

Query Monitor is an excellent tool for looking at what is going on under the hood. It is like Dev Tools on steroids. It shows you the queries that are running on the current page, as well as PHP errors, slow API responses, and lots of other useful information.

Start with the PHP errors pane to rule out PHP problems. Are there undefined variables? Did you forget a colon somewhere? The location column will show you the file and line that needs to be fixed.

Next, click on the Queries tab and look at the actual queries. You can sort by time and filter by Component or Plugin (caller). Look for the query that is running slow or causing problems. This should help you narrow down your search to the specific query that is causing issues.

Image of Query Monitor. Sort by time to find slow queries.
 Query Monitor – Sort by time to find slow queries. 

Query problems will most likely occur with 3rd-party plugins or custom components. If it is a WordPress Core component, your options are slim. You can either roll back to an older version that doesn’t have the problem, or hack the core files (NOT recommended). The good news is that in the unlikely event it’s a core file, it will probably be patched in a future update. You can even be a hero and file a bug report to help the community improve WordPress!

Now Comes the “Fun” Part

Once you know the query that is slowing you down, you can debug and optimize it using standard SQL practices. Is it querying a core WordPress database table or a custom table added by a plugin? Are you looping through tens of thousands of rows of data? Are you using multiple joins? Are you using indexes properly? You can use various tools like phpMyAdmin, MySQLWorkbench, Querious, or Sequel Pro, to look at the table structure and run test queries.

Occasionally you may find queries that run slow due to the data they need to access. You can try increasing the execution time on your web server, which will decrease 500 errors. However, it won’t speed up your site and can negatively impact other users accessing your site. A better solution might be to preprocess your data and serve cached content. Your code may need extensive changes. If you are using a 3rd-party plugin, it could be a red flag that you should find a different plugin.

Conclusion

Understanding and optimizing SQL queries can be frustrating and challenging. Fortunately, WordPress is generally well-optimized and stable, and you will seldom have problems with the core files. Most issues you will encounter will be with 3rd- party plugins or your own code. Now you have a few tools and tips to help you narrow down and identify specific problems with your queries. Good luck!

Resources