Slow WordPress Queries


I love working with WordPress, it makes so many things easier for clients and it gives me as a web developer a great deal of flexibility to extend and offer to clients what they need for their websites. Unfortunately there are also times working with WordPress makes me scratch my head, this was one of those times.

One of my clients WordPress websites seemed to be crawling, it was just not loading quickly at all yet nothing seemed to have changed on the site. There had been a slight change with the main menu on the site so I thought perhaps there was a problem with the new menu options and I decided to log the slow MySQL queries to see I could find out.

After awhile I came back to look at the the MySQL slow queries log and to my surprise I found the following slow query over and over again: SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';

I was shocked by the fact that the main problem with the site seemed to be the main options table. After doing a little Googling on the query I came across this excellent post ”WordPress wp_options table autoload micro-optimization” and decided to try adding an index to the wp_options table and see if it helped speed up the clients WordPress website.

While the post I linked to used the command line, I actually used PHPMyAdmin to add the index to the autoload column. I am always cautious when working with the WordPress database, so I made sure I had a backup copy of the database before messing with it, and added the index. I then cleared the MySQL slow queries log and waited. Of course I checked the clients website to make sure everything looked ok and I was optimistic this had fixed the problem. The site was already responding much faster than it was earlier.

After a few hours I went and checked the slow queries log again and the slow queries on the wp_options table had disappeared. There are still some slow queries to deal with including one with WordPress SEO from Yoast and transients but the wp_options autoload query seems to be fixed for now.

What I am not sure of, is why does the WordPress team not have an index on the autoload column? Considering how much the options table is used to store plugin and theme data (with right or wrong I will not comment) but the fact that the options table is used for saving WordPress transients would suggest to me that perhaps that column should be set with an index by default.

If your WordPress website is starting to seem a little sluggish this is something else you might want to look into doing to speed your website up. If you are not comfortable with working with your WordPress database feel free to contact me I will be glad to help.

Categories: wordpress