Published: May 27, 2022
Why is my website so slow?
Many things can slow down your WordPress website, including 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. If you are running into Slow SQL queries in your WordPress blog, Envano is here with the speedy solution.
For a quick start, jump to Query Monitor below.
What are Queries?
Most modern websites use a stack of applications, including the web server’s operating system, a web server, a database, and 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 slow SQL queries issue, you need to be able to repeat the problem — when and where is it happening? Is it only specific pages that are slow? Only when you are logged in to the backend? Is it only at certain times of the day? Are you using CDN or web app firewalls 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.
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 for Slow SQL Queries
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.
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? What about 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.
Slow SQL Queries: Final Thoughts
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. But if you’re still struggling, fill out our contact form and let’s chat!