How to Optimize WordPress Database Performance & Eliminate Transient Bloat
How to Optimize WordPress Database Performance & Eliminate Transient Bloat
Over time, active WordPress installations accumulate hundreds of thousands of orphaned rows: post revisions, auto-drafts, orphaned post meta, spam comments, and expired transients in wp_options. This database bloat causes slow page loads, high server memory consumption, and sluggish search queries.
This guide provides an actionable runbook for safely cleaning, indexing, and optimizing your WordPress MySQL database.
1. Understanding Common Sources of Database Bloat
| Table Name | Source of Bloat | Performance Impact |
|---|---|---|
wp_options |
Autoloaded rows and expired transients | Slows every page load because autoloaded options load on every single request. |
| wp_posts | Post revisions, auto-drafts, and trashed posts | Inflates table size, slows queries, and degrades index caching. |
| wp_postmeta | Orphaned metadata referencing deleted posts | Wasteful disk space and slow metadata joins. |
| wp_comments | Spam, unapproved, and trashed comments | Slows moderation queue and comment count queries. |
> mysqldump -u root -p database_name > backup_before_cleanup.sql > ``` --- ## 2. Cleaning Expired Transients from `wp_options` Transients are temporary cached values saved in the `wp_options` table. While WordPress clears transients when requested after they expire, transients that are never accessed again can sit in the database indefinitely. ### Inspecting Total Transients Run this query to check how many transient rows currently exist in `wp_options`:sql
SELECT COUNT() FROM wpoptions WHERE optionname LIKE ‘transient%’;
### Safely Deleting Expired Transients
Run the following query to delete all expired timeout records and their corresponding transient payloads:
sql
— Step 1: Delete expired transient timeouts
DELETE a, b FROM wpoptions a
LEFT JOIN wpoptions b ON b.optionname = REPLACE(a.optionname, ‘timeout’, ”)
WHERE a.optionname LIKE ‘transienttimeout%’
AND CAST(a.optionvalue AS UNSIGNED) < UNIXTIMESTAMP();
— Step 2: Delete expired site transients in multisite environments
DELETE a, b FROM wpsitemeta a
LEFT JOIN wpsitemeta b ON b.metakey = REPLACE(a.metakey, ‘timeout’, ”)
WHERE a.metakey LIKE ‘sitetransienttimeout%’
AND CAST(a.metavalue AS UNSIGNED) < UNIXTIMESTAMP();
---
## 3. Auditing and Reducing Autoloaded Options
Every option in `wp_options` with `autoload = 'yes'` is fetched and loaded into memory on **every single WordPress request**, including the REST API.
If your autoloaded options exceed **800 KB - 1 MB**, site performance degrades noticeably.
### Check Total Size of Autoloaded Options
sql
SELECT
ROUND(SUM(LENGTH(optionvalue)) / 1024, 2) AS autoloadsizekb,
COUNT() AS totalautoloadrows
FROM wpoptions
WHERE autoload = ‘yes’;
### Find the Top 10 Largest Autoloaded Options
sql
SELECT
optionname,
ROUND(LENGTH(optionvalue) / 1024, 2) AS sizekb
FROM wpoptions
WHERE autoload = ‘yes’
ORDER BY LENGTH(optionvalue) DESC
LIMIT 10;
> [!TIP]
> If you find inactive plugins leaving behind massive serialized cache objects, you can switch their autoload status to `no`:
> ```sql
> UPDATE wp_options SET autoload = 'no' WHERE option_name = 'heavy_plugin_cache';
> ```
---
## 4. Purging Post Revisions and Auto-Drafts
By default, WordPress stores every autosave and draft revision indefinitely. A post edited 50 times creates 50 separate rows in `wp_posts`.
### Count Existing Revisions
sql
SELECT COUNT() FROM wpposts WHERE posttype = ‘revision’;
### Delete All Revisions and Associated Meta
sql
— 1. Remove revisions from wpposts
DELETE FROM wpposts WHERE posttype = ‘revision’;
— 2. Remove orphaned postmeta referencing deleted revisions
DELETE pm FROM wppostmeta pm
LEFT JOIN wpposts wp ON wp.ID = pm.postid
WHERE wp.ID IS NULL;
— 3. Remove orphaned term relationships
DELETE tr FROM wptermrelationships tr
LEFT JOIN wpposts wp ON wp.ID = tr.objectid
WHERE wp.ID IS NULL;
### Limit Future Revisions in `wp-config.php`
To prevent revisions from growing out of control in the future, define a limit in `wp-config.php`:
php
// Limit post revisions to the 5 most recent per post
define(‘WPPOSTREVISIONS’, 5);
// Increase autosave interval from 60 seconds to 180 seconds
define(‘AUTOSAVEINTERVAL’, 180);
---
## 5. Optimizing InnoDB Tables and Reclaiming Disk Space
After deleting thousands of stale rows, the MySQL tables still retain the allocated disk space. Running `OPTIMIZE TABLE` reclaims fragmented space and rebuilds table indexes.
Run this SQL statement across your core WordPress tables:
sql
OPTIMIZE TABLE
wpcommentmeta,
wpcomments,
wplinks,
wpoptions,
wppostmeta,
wpposts,
wptermmeta,
wpterms,
wptermrelationships,
wptermtaxonomy,
wpusermeta,
wpusers;
---
## 6. Automating Weekly Maintenance via WP-Cron
You can automate transient and revision cleanup via a lightweight function hooked into WordPress cron:
php
/**
Register a weekly scheduled database health task
*/
addaction(‘xyphlaweeklydbcleanup’, function() {
global $wpdb;
// Purge expired transients
$wpdb->query(“
DELETE a, b FROM {$wpdb->options} a
LEFT JOIN {$wpdb->options} b ON b.optionname = REPLACE(a.optionname, ‘timeout’, ”)
WHERE a.optionname LIKE ‘transienttimeout%’
AND CAST(a.optionvalue AS UNSIGNED) < UNIXTIMESTAMP()
“);
// Purge trashed posts older than 30 days
$wpdb->query(“
DELETE FROM {$wpdb->posts}
WHERE poststatus = ‘trash’
AND postmodified < DATESUB(NOW(), INTERVAL 30 DAY)
“);
});
if (!wpnextscheduled(‘xyphlaweeklydbcleanup’)) {
wpscheduleevent(time(), ‘weekly’, ‘xyphlaweeklydb_cleanup’);
}
“`
Was this technical article helpful?
Help us continuously refine Xyphla documentation.