In this post, we will break down and share how we optimize the WordPress database, and database queries when working on site speed.
A slow WordPress database or slow queries will typically manifest in areas of WordPress that aren’t cached. Like the WordPress backend, checkout pages in WooCommerce, or membership pages on a membership site.
There’s no magic in site speed improvement, and speeding up the database end of WordPress is the same. Ultimately, how you can speed up WordPress data queries could be summarized as:
- Use better hosting;
- Use object caching powered by Redis or Memcached (memory-based database caching);
- Reduce the load on the site and database;
- Configure the database in a best practices fashion.
How to Speed Up WordPress Database Queries
The recommendations below can be a bit technical, so please post in the comments if you have a question or need anything clarified.
Use a Good Host That Ideally Has Memcached or Redis Caching
Having a high-quality, reliable hosting provider that supports Memcached or Redis caching is crucial. Memcached and Redis are memory caches that can be used for Object Caching – basically, WordPress database caching.
Redis is probably faster in most cases, but Memcached is generally more widely available. These are applications installed on the server or hosting itself.
If you have a VPS that you’re in control of. You should be able to install one of these apps on it.
Use Object Caching
Object caching is a type of database caching that can dramatically speed up sites with heavy database operations. WooCommerce checkout and cart operations, order management on the backend. And almost everything that happens behind the logon on a membership site is all database heavy operations that will benefit from Object Caching.
The object cache sits in front of the database and can answer previous database queries (if in the cache) without talking to the database.
Your host will need to support Redis or Memcached to use object caching. And we typically use the Redis Object Cache plugin from Till Kruss inside WordPress to power the caching.
Use the Highest Version of PHP the Site Supports
PHP is the programming language WordPress is built on. New versions of PHP get released regularly (every 6-12 months).l And each version typically is 10-30% faster than the previous version.
Using the highest version of PHP that your site supports can dramatically speed up database-related operations.
Reduce the Load by Using Page Caching
You pretty much can’t run a WordPress site without Page Caching. With Page Caching in place, pages are pre-built before the visitor hits the website. Which is a great way to speed up WordPress data queries.
The PHP processing and database lookups required to generate the HTML file are all done in advance and stored in the page cache. When the visitor hits the website, the server provides the HTML file immediately. So the user experiences a faster site, and the load on the server is dramatically reduced. Typically, it’ll take 1-4 seconds to generate a page from scratch. Whereas a cached page is available in a few hundred milliseconds (0.2-0.5 seconds)
WP Rocket is one of the best caching plugins on the market. It includes many great features. So it stands out as the plugin we highly recommend to everyone who wants to speed up WordPress data queries and improve their website’s performance.
Reduce the Load by Using Cloudflare CDN
Even if you use a low-quality host, Cloudflare can greatly decrease your site’s load times, even on the free plan.
If you can’t use Cloudflare, use a CDN service (one that has image optimization built-in, like Bunny CDN). CDN is very useful in speeding up the response of static assets such as CSS, JS, images, and fonts.
Make Sure Your Database Is Using the InnoDB Storage Engine for All Tables
InnoDB and MyISAM are “storage engines” used by MySQL – essentially the format the database stores its database. MyISAM was a default table type until MySQL 5.5.5 was introduced in 2010. InnoDB’s tables are faster than MyISAM, so ensuring the tables use the InnoDB storage engine can dramatically speed up queries.
There are several differences between the two, but in simple terms. MyISAM tables will lock a database table while it’s being written to. This means that these databases write operations start to queue on a busy site and cause delays in processing, which manifest as slower loading to the user.
Think of the database table as an Excel spreadsheet where if one person has it open. Another person can’t make any edits.
InnoDB tables only lock the row in the database table that’s being written to. So there’s little to no database queuing. It’s like using a shared Google Sheet that multiple users can work on at once.
Converting from MyISAM tables to InnoDB tables can give you a solid speed boost. Particularly in the backend and on higher traffic sites.
Delete Expired Transients for Your Database
The transients API on WordPress allows developers to store temporary information in the WordPress database and assign it an expiration time, after which it will be deleted. This eases server load and improves WordPress performance.
Sometimes, transients expire or disappear before their set timeframe or don’t have the expiring time. Old and expired transients can increase the site load and negatively influence performance. Several plugins can delete expired transients, like WP Rocket and WP-Optimize.
Further, help….
I hope you found this post useful.
If you’re looking for help specifically with high database load, our Consult Service is probably the service that can help you. If you’re unsure, head to the homepage and submit a free site speed audit request.