MySQL indexes on WordPress tables
An index lets MySQL find rows without reading the whole table. WordPress core tables already have the indexes that core queries need; slow queries usually come from plugin code that filters by something core never indexed, most often a meta value. This post shows how to read the existing indexes, when an extra one helps and how to design indexes for your own tables.
Problem: A query on wp_postmeta by key and value is slow on a large site, and EXPLAIN shows that MySQL reads far more rows than it returns.
Solution: List the indexes with SHOW INDEX, check the plan with EXPLAIN, add a composite index on (meta_key, meta_value) with prefix lengths, and check the plan again. For your own tables, design the indexes after the WHERE and ORDER BY of the queries.
The examples use the wp_ prefix. Reading plans is covered in EXPLAIN for slow MySQL queries.
What is already there
SHOW INDEX FROM wp_postmeta; -- PRIMARY (meta_id), post_id, meta_key (prefix 191) SHOW INDEX FROM wp_posts; -- PRIMARY (ID), post_name, type_status_date (post_type, post_status, post_date, ID), -- post_parent, post_author
The type_status_date index already serves the most common query, “published posts of a type, newest first”. Adding another index on the same columns only slows down writes.
A meta value index
EXPLAIN SELECT post_id FROM wp_postmeta WHERE meta_key = '_ha_featured' AND meta_value = '1'; -- key: meta_key, rows: 48000 ALTER TABLE wp_postmeta ADD INDEX ha_key_value (meta_key(191), meta_value(32)); EXPLAIN SELECT post_id FROM wp_postmeta WHERE meta_key = '_ha_featured' AND meta_value = '1'; -- key: ha_key_value, rows: 120
meta_value is LONGTEXT, so only a prefix can be indexed. 32 characters are enough for flags, IDs and dates. The index helps exact matches and prefix searches (LIKE 'abc%'), not LIKE '%abc%' and not numeric comparisons on text (CAST(meta_value AS SIGNED) > 10).
Run ALTER TABLE on a large table outside peak hours and after a backup. WordPress updates do not remove extra indexes, but write them down: after a migration to a new server they are easy to lose.
Indexes for your own table
CREATE TABLE wp_ha_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
event_type VARCHAR(50) NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY user_type_created (user_id, event_type, created_at),
KEY created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- uses user_type_created for the WHERE and the ORDER BY
SELECT id, created_at FROM wp_ha_events
WHERE user_id = 7 AND event_type = 'login'
ORDER BY created_at DESC LIMIT 20;
A composite index works from the left: this one also serves queries by user_id alone, but not by event_type alone. Put the columns compared with = first and the column used for a range or sorting last.
NOTE: Every index costs disk space and time on every INSERT and UPDATE. Add one for a query you measured, check with EXPLAIN that MySQL uses it, and remove indexes that nothing uses. If the same meta value query runs on every page, caching its result is often better than any index.