Find redundant indexes that are a prefix of another index in MySQL
An index on (a, b, c) can answer every lookup an index on (a) can. This finds the short indexes hiding behind longer ones.
The task
MySQL's sys schema ships a view for this: sys.schema_redundant_indexes compares the indexes on each table and reports one as redundant when another index starts with the same columns in the same order, or has exactly the same columns. An index on (customer_id) next to one on (customer_id, created_at) doubles the write cost on customer_id for no read benefit, because the longer index serves every lookup the shorter one can.
The snippet
Written for MySQL.
SELECT table_schema, table_name,
redundant_index_name, redundant_index_columns, redundant_index_non_unique,
dominant_index_name, dominant_index_columns, dominant_index_non_unique,
subpart_exists, sql_drop_index
FROM sys.schema_redundant_indexes
ORDER BY table_schema, table_name, redundant_index_name;
仕組み
The view is built on information_schema.STATISTICS: it flattens each index into a column list in SEQ_IN_INDEX order and pairs indexes on the same table where one list is a prefix of, or equal to, the other. The *_non_unique columns show 0 for a unique index. A unique index is never reported as redundant to a non-unique one, since dropping it would drop the rule. The reverse can happen: with UNIQUE (a) in place, an index on (a, b) is reported as redundant to it, because a lookup on a already finds at most one row. subpart_exists = 1 means a prefix-length column such as name(10) is involved, so compare the definitions by hand. sql_drop_index holds the generated ALTER TABLE ... DROP INDEX. Exact duplicates are reported once, with one index as redundant to the other. Before dropping, search the code for FORCE INDEX or USE INDEX hints naming the index, because those queries fail once it is gone.
Variations
Without the sys schema: prefix pairs from information_schema
SELECT s.TABLE_SCHEMA, s.TABLE_NAME,
s.INDEX_NAME AS redundant_index, s.cols AS redundant_columns,
d.INDEX_NAME AS dominant_index, d.cols AS dominant_columns
FROM (SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME, MIN(NON_UNIQUE) AS NON_UNIQUE,
GROUP_CONCAT(CONCAT(COLUMN_NAME, IFNULL(CONCAT('(', SUB_PART, ')'), ''))
ORDER BY SEQ_IN_INDEX) AS cols
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE() AND INDEX_TYPE = 'BTREE'
GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
HAVING SUM(COLUMN_NAME IS NULL) = 0) s
JOIN (SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME,
GROUP_CONCAT(CONCAT(COLUMN_NAME, IFNULL(CONCAT('(', SUB_PART, ')'), ''))
ORDER BY SEQ_IN_INDEX) AS cols
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE() AND INDEX_TYPE = 'BTREE'
GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME) d
ON d.TABLE_SCHEMA = s.TABLE_SCHEMA
AND d.TABLE_NAME = s.TABLE_NAME
AND d.INDEX_NAME <> s.INDEX_NAME
AND LEFT(d.cols, CHAR_LENGTH(s.cols) + 1) = CONCAT(s.cols, ',')
WHERE s.NON_UNIQUE = 1
ORDER BY s.TABLE_NAME, s.INDEX_NAME;
Questions that come up
Is a prefix index always safe to drop when a longer one exists?
For reads, nearly always: a query that can use (a) can use (a, b, c). Three things to check first. A UNIQUE index on (a) enforces something the longer index does not, so keep it. A foreign key on a needs some index that leads with a, and InnoDB will use the longer one, but DROP INDEX fails if no other index qualifies. And the longer index is wider, so a range scan on a alone reads more pages through it than through the short one; on a hot query that difference can be measurable.
Does every MySQL server have the sys schema?
MySQL 5.7.7 and later install it by default, including 8.0 and 8.4, unless someone dropped it or the server was set up with it disabled. Where it is missing, the information_schema variant finds strict prefix pairs for the current database. It skips functional indexes and does not list exact duplicates; to find those, change the join condition to d.cols = s.cols AND d.INDEX_NAME < s.INDEX_NAME.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.