Find and fix oversized autoloaded options
Every WordPress request starts by loading all options marked as autoload = 'yes' in one query. They are kept in memory (in the object cache under the key alloptions), so later get_option() calls for them cost nothing. The problem is that plugins create options with autoload on by default, also for data needed only on one admin screen, and some store large arrays there. After years of plugins coming and going, the autoloaded data can grow to megabytes that are loaded on every page view.
Problem: The autoloaded options of a site add up to more than a megabyte, much of it from old plugins or data needed only in the admin.
Solution: Measure the total and find the largest autoloaded options with SQL, delete options of plugins that are gone, switch autoload off for large options that are not needed on every request, and create your own rarely used options with autoload off.
Measuring
-- total size of the autoloaded options SELECT ROUND( SUM( LENGTH( option_value ) ) / 1024 ) AS kb FROM wp_options WHERE autoload = 'yes'; -- the 20 largest ones SELECT option_name, ROUND( LENGTH( option_value ) / 1024 ) AS kb FROM wp_options WHERE autoload = 'yes' ORDER BY LENGTH( option_value ) DESC LIMIT 20;
Replace wp_ with the table prefix of the site. Up to a few hundred kilobytes is normal. Above a megabyte it is worth a look, especially with a persistent object cache: the whole alloptions array is read from Redis or Memcached on every request, and very large values can exceed Memcached’s 1 MB item limit.
Deciding what to change
Look at the names. They usually start with the plugin’s prefix:
- Options of plugins that are no longer installed: delete them (after a backup).
- Large options of active plugins that are used only in the admin or on one page (import logs, statistics, cached API responses): switch autoload off.
- Options used on every front-end request (
siteurl,active_plugins, theme settings,rewrite_rules): leave them. With autoload off, each of them becomes a separate query on every page.
UPDATE wp_options SET autoload = 'no' WHERE option_name = 'some_plugin_import_log'; DELETE FROM wp_options WHERE option_name LIKE 'removed_plugin\_%';
After the change, clear the object cache (wp cache flush) so the cached alloptions array is rebuilt.
Your own options
<?php // add_option( name, value, deprecated, autoload ) add_option( 'ha_import_log', array(), '', 'no' ); // update_option() can set it too (WordPress 4.2+); the third argument applies when the option is created update_option( 'ha_report_cache', $report, false );
Data that expires, like cached API responses, belongs in transients rather than in options: set_transient() with an expiration time is not autoloaded and is removed automatically.
Update (2024): WordPress 6.6 changed the stored values to on/off (and auto for options without an explicit choice), stopped autoloading very large new options automatically, and shows a warning in Site Health when the autoloaded options exceed 800 KB. In SQL on 6.6+, check for autoload IN ('yes', 'on', 'auto-on', 'auto').
NOTE: Switching off autoload for an option that a plugin reads on every page view makes the site slower, not faster: one extra query per option per request. When in doubt, check with the Query Monitor plugin which options are queried individually after the change.