DIY guide

How To Speed Up & Optimize WordPress Database Queries

Updated October 15, 2026

Every time WordPress builds a page that is not served from cache, it runs database queries. When those queries are slow or there are too many of them, your server takes longer to respond, and that delay is added to every other speed metric. This guide shows how to find slow queries and clean up your database safely.

Back up first. Make a full backup of your files and database before you delete or change anything below.

1. Find out what is slow

Install the free Query Monitor plugin on a staging site, or on your live site for a short test. Load a slow page while logged in, then open the Query Monitor panel in the admin bar. Look at three things:

  • Queries by component: which plugin or theme runs the most queries, or the slowest ones.
  • Slow queries: sort by time and note anything that takes far longer than the rest.
  • Duplicate queries: the same query repeated many times points to inefficient code.

Deactivate Query Monitor when you are done.

2. Remove database clutter

Over time a database collects data that nothing needs any more:

  • Old post revisions and auto-drafts
  • Spam and trashed comments
  • Expired transients (temporary cached values)
  • Leftover data from plugins you deleted
  • On WooCommerce sites, old customer sessions

A cleanup plugin such as WP-Optimize can remove most of this from the dashboard. If you have WP-CLI access, these two commands are safe and quick:

wp transient delete --expired
wp db optimize

To stop revisions from piling up again, limit them in wp-config.php, above the line that says to stop editing:

define( 'WP_POST_REVISIONS', 5 );

3. Check autoloaded options

WordPress loads every “autoloaded” option from the options table on every single request. If a plugin stores a large amount of data there, every page pays for it. Open Tools → Site Health: it warns you when autoloaded options become too large. If it does, find which plugin owns the biggest options and ask whether you still need it.

4. Add a persistent object cache

If your host offers Redis or Memcached, enable it. An object cache keeps the results of repeated queries in memory, so WordPress does not ask the database the same question on every request. It works best together with page caching, not instead of it.

5. Fix the cause, not only the symptom

  • Remove or replace plugins that generate heavy queries, such as some related-posts, statistics and live-search plugins.
  • Keep PHP and your database server on current, supported versions.
  • If you run WooCommerce, check whether High-Performance Order Storage is enabled in WooCommerce settings. It stores orders in dedicated tables and suits stores with many orders.

6. Measure the result

Page caching hides database problems, so test pages that are not cached: the WordPress admin, the cart and checkout, or search results. Compare server response time before and after your changes. For the next step, read how to reduce TTFB.

Prefer to have this done for you?

We test your site, fix what slows it down, and send a before-and-after report.

Want your website faster? Start with a free speed assessment.

Request my free assessment →