When a WordPress site gets slower over the years, people usually blame the theme, the host or the plugins. Often the real culprit is the database. Every post revision, expired transient, spam comment and leftover plugin setting adds rows, and some of that clutter is loaded on every single page request.
This guide shows you how to optimize a WordPress database in MySQL or MariaDB, from simple cleanups anyone can do to the deeper fixes that make the biggest difference.
Before anything else: take a backup
Database work is one of the few things that can break a site instantly. Before you change or delete anything:
- Export the database from phpMyAdmin (Export > Quick > SQL) or with your host's backup tool.
- Download a copy to your computer.
- Ideally, test changes on a staging copy first.
Our guide on backing up a website in cPanel or DirectAdmin walks through this, and WordPress staging with Softaculous shows how to make a safe test copy.
How WordPress uses the database
A standard WordPress install has around a dozen core tables. The ones that tend to get bloated are:
- wp_posts: posts, pages, revisions, menu items and many plugin records
- wp_postmeta: extra data attached to posts, often huge on WooCommerce sites
- wp_options: site settings, plugin settings and transients
- wp_comments and wp_commentmeta: including spam and trashed comments
Your table prefix might be different from wp_; check $table_prefix in wp-config.php.
Step 1: Clean up revisions, trash and spam
WordPress saves a revision every time you update a post. On a site with years of editing, revisions can outnumber real posts many times over.
- Empty the trash for posts, pages and comments.
- Delete spam comments in bulk.
- Remove old revisions using a reputable plugin such as WP-Optimize or Advanced Database Cleaner, or with WP-CLI.
To stop revisions piling up again, add this to wp-config.php:
define( 'WP_POST_REVISIONS', 5 );
That keeps the five most recent revisions per post, which is plenty for most people.
Step 2: Clear expired transients
Transients are temporary values plugins store in wp_options (when there's no persistent object cache). Expired ones are supposed to be cleaned up, but they often linger. With WP-CLI you can run:
wp transient delete --expired
Most optimization plugins have a one-click option for this as well.
Step 3: Fix autoloaded options (the big one)
This is the fix most people miss. Rows in wp_options flagged with autoload are loaded into memory on every page request, whether they're needed or not. Plugins you uninstalled years ago may still have large autoloaded settings sitting there.
To see how much autoloaded data you have, run this in phpMyAdmin's SQL tab (adjust the prefix):
SELECT SUM(LENGTH(option_value)) AS autoload_bytes
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto-on', 'auto');
WordPress 6.6 introduced newer autoload values, which is why the query checks several. Recent WordPress versions also flag large autoloaded data in Tools > Site Health.
To find the largest offenders:
SELECT option_name, LENGTH(option_value) AS size
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto-on', 'auto')
ORDER BY size DESC
LIMIT 20;
Look for options belonging to plugins you no longer use. Those can usually be deleted, or set to not autoload. If you aren't sure what an option belongs to, search its name online or leave it alone. Deleting an active plugin's settings can reset or break it.
Step 4: Remove leftovers from old plugins
Many plugins leave their own tables behind when you delete them. In phpMyAdmin, scan the table list for names that clearly belong to plugins you removed (often with the plugin name in the table name). Export those tables first, then drop them.
Step 5: Optimize tables
Over time, deleted rows leave unused space in tables. Running OPTIMIZE TABLE rebuilds a table and reclaims that space. In phpMyAdmin, tick the tables and choose "Optimize table" from the dropdown.
For InnoDB tables (the default today), optimization rebuilds the table and its indexes. It is generally safe, but it can lock or slow large tables while it runs, so do it during quiet hours on busy sites. Also, don't expect miracles: on a healthy InnoDB database, optimizing gives modest gains compared with fixing autoload or slow queries.
Step 6: Convert old MyISAM tables to InnoDB
Very old WordPress sites may still have MyISAM tables. InnoDB supports row-level locking and crash recovery and is the better choice for nearly all WordPress sites. In phpMyAdmin you can see each table's engine in the table list. To convert:
ALTER TABLE wp_posts ENGINE=InnoDB;
Back up first and convert one table at a time.
Step 7: Find slow queries
Cleaning up helps, but sometimes one plugin runs a single terrible query. To find it:
- Query Monitor plugin: shows every query on a page, how long each took, and which plugin or theme ran it.
- MySQL slow query log: on a VPS, enable
slow_query_logwith along_query_timeof one second or less to log anything slow.
Once you know which plugin is responsible, you can update it, change its settings, replace it, or (for developers) add a suitable index.
Server-level tuning (VPS owners)
If you manage your own server, the most important MySQL/MariaDB setting for WordPress is usually innodb_buffer_pool_size, which controls how much data and index information stays in RAM. On a server dedicated mainly to the database, it is commonly set to a large share of available memory; on a server that also runs PHP and a web server, keep it more modest so everything fits. Tools like MySQLTuner can suggest starting points based on your actual usage.
On shared hosting, these settings are managed for you, which is one reason why application-level cleanup matters so much there.
Keep it clean going forward
- Limit revisions in
wp-config.php. - Schedule a monthly cleanup with your optimization plugin.
- Delete plugins you don't use instead of just deactivating them.
- Consider a persistent object cache for busy dynamic sites.
- Check Site Health occasionally for autoload warnings.
Wrapping up
Optimizing a WordPress database isn't about a single magic button. Back up first, clear the obvious clutter, then go after the real performance killers: oversized autoloaded options and slow queries. A lean database makes every uncached page, every admin screen and every checkout a little faster, and it keeps your backups smaller too.