Duplicate and redundant indexes: find them and drop them safely
Every extra index slows down writes and uses memory. Here's how to find the ones that do nothing in MySQL and PostgreSQL, and how to remove them without risk.
Indexes speed up reads, but every one has a cost. Each INSERT, UPDATE and DELETE has to maintain every index on the table, and indexes compete with data for memory. An index that duplicates another one gives you all of the cost and none of the benefit.
Two kinds of waste
Duplicate: two indexes on exactly the same columns in the same order.
KEY idx_city (city)
KEY idx_city_2 (city) -- identical: one can go
Redundant: an index whose columns are the leading columns of another index.
KEY idx_customer (customer_id)
KEY idx_customer_created (customer_id, created_at) -- also serves lookups on customer_id alone
The second index can do everything the first one does, because an index on (a, b) can be used for lookups on a alone. So idx_customer is redundant.
Exceptions: never drop a primary key or a unique index because it looks redundant, because they enforce rules about your data, not just speed. And keep an index that a query hint names explicitly (FORCE INDEX), or that query will fail.
Finding them in MySQL
SELECT table_schema, table_name, index_name,
GROUP_CONCAT(column_name ORDER BY seq_in_index) AS columns, MIN(non_unique) = 0 AS is_unique
FROM information_schema.statistics
WHERE table_schema NOT IN ('mysql','sys','performance_schema','information_schema')
GROUP BY table_schema, table_name, index_name
ORDER BY table_schema, table_name, columns;
Sorting by columns puts duplicates and prefixes next to each other. The sys schema also has sys.schema_redundant_indexes, which does the comparison for you.
Finding them in PostgreSQL
PostgreSQL doesn't stop you creating identical indexes either. This lists indexes with identical definitions on the same table:
SELECT indrelid::regclass AS table_name, array_agg(indexrelid::regclass) AS indexes
FROM pg_index
GROUP BY indrelid, indkey, indclass, indexprs::text, indpred::text
HAVING count(*) > 1;
Also look for indexes that are never used:
SELECT relname, indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes ORDER BY idx_scan, pg_relation_size(indexrelid) DESC;
An idx_scan of zero since statistics were last reset is a strong hint. Check replicas too, because each server keeps its own counters.
Dropping safely
MySQL 8: make it invisible first. An invisible index is still maintained but the optimiser ignores it, so you can test the effect of dropping it and undo instantly:
ALTER TABLE customers ALTER INDEX idx_city_2 INVISIBLE;
-- watch query times for a few days, then:
ALTER TABLE customers DROP INDEX idx_city_2;
-- or, if something got slower:
ALTER TABLE customers ALTER INDEX idx_city_2 VISIBLE;
PostgreSQL: drop concurrently. A plain DROP INDEX takes a lock that blocks queries on the table. DROP INDEX CONCURRENTLY doesn't (it can't run inside a transaction block):
DROP INDEX CONCURRENTLY idx_city_2;
Keep the CREATE INDEX statement handy so you can put it back.
SQLNirvana's Schema tab lists duplicate and redundant indexes on every server, with the size of each and the statement to remove it.