Why WordPress Databases Slow Down Over Time
As a WordPress site grows, the MySQL/MariaDB database accumulates thousands of obsolete post revisions, spam comment metadata, orphaned term relationships, and expired transients. Over time, unindexed queries take hundreds of milliseconds to execute, directly degrading your server's Time to First Byte (TTFB).
1. Cleaning Autoloaded Data in wp_options
WordPress loads all rows where autoload = 'yes' into memory on every single page request. If total autoloaded data exceeds 800KB, your server response time will suffer significantly.
SQL Query to Identify Heavy Autoloaded Options:
SELECT option_name, length(option_value) AS option_value_length
FROM wp_options
WHERE autoload='yes'
ORDER BY option_value_length DESC
LIMIT 20;
SQL Query to Disable Autoload on Non-Critical Plugin Data:
UPDATE wp_options SET autoload = 'no' WHERE option_name = 'heavy_deactivated_plugin_cache';
2. Deleting Expired Transients
Transients are temporary cache records stored in wp_options. Many plugins fail to delete expired transients, leaving millions of stale rows behind:
-- Delete expired transient timeouts
DELETE FROM wp_options WHERE option_name LIKE ('_transient_timeout_%') AND option_value < UNIX_TIMESTAMP();
-- Delete matching transient data
DELETE FROM wp_options WHERE option_name LIKE ('_transient_%')
AND option_name NOT IN (SELECT CONCAT('_transient_', SUBSTRING(option_name, 20)) FROM (SELECT * FROM wp_options) AS temp WHERE option_name LIKE ('_transient_timeout_%'));
3. Limiting Post Revisions in wp-config.php
By default, WordPress stores an infinite number of revisions for every post edit. Limit revisions to 3 by adding this line to wp-config.php:
define('WP_POST_REVISIONS', 3);
define('EMPTY_TRASH_DAYS', 7);

