Troubleshooting slow page speed on your wordpress site

If you think that your WordPress pages are loading slowly, it is time to do some troubleshooting to improve the page speed. Do not wait in doing the troubleshooting and let your site’s success be its downfall. Whether you’re a developer or a website owner, you want your site to handle any type of traffic without slowing down.

I will walk you through steps you can take and tools you can use to validate your site so the site will load the web pages so it will satisfy you and your visitors. So how do you troubleshoot to find out where your bottlenecks are, first I will touch on some of the major performance quirks that sites experience as they grow and I will show you how to check what may be slowing down your server resources.

Understanding Site Traffic

Some Plugins and themes can perform fine at certain traffic levels, but as more visitors come to your site and you have peak times hits, server resources can be pushed to the limit. At times a server has more work to do than it can accomplish, and it will become overloaded. What do you need to do, you ensure that the request to your site uses fewer server resources and uses as little effort as possible to render and serve the web page. To do this you need to make sure that you serve less, serve more efficiently, or both. So what can you do to achieve this, I will be showing you how to tune the backend (e.g. MySQL) and the frontend.

The Backend (MySQL)

WordPress uses MySQL in the backend to serve your webpages, MySQL uses the most resources on your server and without MySQL, your website will not work. How does WordPress PHP code interact with MySQL, in principal PHP sends a query to the MySQL server, the PHP process waits until the database responds with data. So the longer a MySQL query takes to run, the longer it will take your entire site to load. That’s why it is important to minimizing each page load, as this is one of the most effective tasks you can do to get your site loading faster for your site visitors.

You need to identify where your slowest queries come from.  For this, you need to add

define( ‘SAVEQUERIES’, true );

to your wp-config.php file, so you can analyze them later with the WordPress Debug Objects plugin, I suggest that you install the WordPress Developer Plugin that will give you all the resources to debug your site, you should install this on your development environment.

With the Debug Objects plugin, you can view and quickly identify which queries take the longest. By removing the plugin that uses queries that they the longest to execute, you will improve your web page speed. If you can not remove the plugin, find an alternative that executes a query in a shorter time. It is all about tried and test tills you find the optimal plugin.

What is a query? A query is a request to create, read, update, or delete (CRUD) data from the database.

Keep track of your Option Table

WordPress uses the Options table to store a lot of data from plugins, themes, and the WordPress core.  The Options table has a column named option_value and it is set to LONGTEXT. In MySQL terms LONGTEXT can store up to 4GB of data in a single row, you should try to avoid storing that much data in one column.

Another issue that can cause slow page speed is the Options table column autoload, so if you’re having page load speed issues, try identifying how many queries are being autoloaded. These queries assume the default table prefix of “wp_”

mysql> SELECT count(*) FROM wp_options WHERE autoload=’yes’;

A good rule of thumb is to make sure that you have fewer than 200 autoloaded queries on any given page.  IF you have more, you would need to take a look at the Options table, it may contain information from plugins and themes that you have used in the past on your site.  To improve the speed you would need to remove all the old options!

This is a very time consuming and detail-oriented work. It’s extremely important for your site from a troubleshooting perspective since it can highlight what is negatively impacting your site’s health, but it doesn’t always result in a dramatic improvement it’s just one piece of the puzzle.

When you have removed old options, you can now analyze the Options table and determine the size of the options that are being autoloaded by using the “sum” and “length“ functions in MySQL:

mysql> SELECT SUM(LENGTH(option_value)) as autoload_size FROM wp_options WHERE autoload=’yes’;

If you’d like to see which options specifically are causing the most trauma, try this:

mysql> SELECT option_name, length(option_value) AS option_value_length FROM wp_options WHERE autoload=’yes’ ORDER BY option_value_length DESC LIMIT 10;

If you have autoload_size in the MBs, you could try to disabling any of the plugins or themes and check that the “autoload” option is set to “no”, if the “autoload” option remains set to “yes” after you disable or the “autoload” remains after you remove a plugin or themes it will continue to plague your site’s performance long after the plugin or theme is gone (this is also true if the option is not autoloaded).

A great way to test if a plugin’s or theme’s options are slowing down page load speed is to first disable the theme or plugin in question, set the offending option’s autoload value to “no,” and then test to determine if your site experiences a better performance.

Important: If you do not have the skills of doing this yourself, then find a developer that can prune your autoloaded options.

Catch the obvious ones first, such as those named after themes and plugins that you no longer use, and please, be sure to backup your database before deleting anything.

Please check the number of options on the table. Although this is a less typical scenario, there’s a chance this might be the single thing causing your site trouble. You can check to see exactly how long it takes to grab all of the autoload options like this (again, this assumes that you are using the table prefix “wp_”):

mysql> SELECT option_name, option_value FROM wp_options WHERE autoload = ‘yes’;

However long that query takes to execute is how much time it’s adding to each page load. While having a lot of options is generally a painful way to run, in the unlikely event that this query is taking the majority of a second or more, one solution is to add an index on the autoload column like this:

mysql> CREATE INDEX autoload ON wp_options (autoload);

If you notice that this doesn’t speed up your query, you can drop the index like this:

mysql> DROP INDEX autoload ON wp_options;

There are a few things to be cautious about when doing this:
  • Adding indexes on a production database can lock your database for extended periods of time. Be sure to wait for a time when traffic is low before making any changes.
  • Adding an index may actually add time to your page load. Before you make any changes, be sure to do some basic benchmarking so you can compare page load speeds before and after.
  • Plugins or themes can cause the cron option to become bloated. You will detect that when you run the query for the largest autoloaded options in which you would see the option_name “cron”.

Watch out for plugins that track data— they may be storing their logs in the wp_options table and reading/writing to them on a per-page load basis and even autoload them. If you are using a plugin to track visitor data with WordPress, you should consider removing the plugin, even if you have a setup with a master/slave database, it’s very common to see WordPress grind to a halt when it’s used to track visitor data at scale. Instead, use a service that allows you to load a JavaScript library that handles the tracking automatically like Google Analytics.

Logging visitor data doesn’t work well on WordPress because MySQL locks its tables significantly longer to perform an INSERT than it does for a SELECT. MySQL is getting requests from visitors who are asking to read from and write to the database — but they can’t access it immediately because it’s locked from the request that was received before them, so they’re stuck waiting.

Other observations on the backend

For some sites, having other bits of information — such as a secondary or micro blogroll in sidebars or footers — can make sense and increase a reader’s engagement, but remember that at scale everything adds up. Consider generating the micro blogroll once and then storing it as a transient for simple retrieval later rather than rebuilding it from scratch each time.

When you delete a plugin or theme, make sure that you select the button “Yes, Delete Data & Files” – this does not necessarily delete all data for the plugin, it gives you a head start on cleaning up the data for a plugin or theme. Some plugins install their own tables, they are not necessarily removed it depends on how well the plugin has been written, so you might need to use phpmyadmin or another DB tool to view the tables for a plugin and delete them manually.

To get even better page speed out of your website, you might want to add Varnish and Memcache to your server, read my article – How To Install Varnish + Memcached to improve your WordPress page load speed

The WordPress Application

It’s really simple these days to run your site through a page speed test and see 100 different dissections of how a site performs. There are several great free options to test your page load speed:

I use New Relic for my site, the first thing you should look at is the time to the first byte when a page is loaded. If your time to the first byte is still more than 0.75 seconds, you should consider spending some more time fine-tuning MySQL and improving the backend.

For more detail on how to speed up your WordPress you can read my article – Do you need to speed up your WordPress site speed

Conclusion

If you not preparing your WordPress site to serve heavy traffic, you can be setting yourself up for a disaster. Especially if it happens when a record number of people are interested in your site or business. If you do not take the steps to optimize your site, your potential customer may move on to a competitor’s site when yours is down as that is easier then wait for your website to work again.

There is a great guide at wpbeginner, that deals with what you can do to boost WordPress speed & performance  or you can check out this guide Best WordPress Hosting by Performance.


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *