Skip to content

Doing a safe index review on a production MySQL table

Indexes accumulate over years and nobody removes them. Here is how to review which ones are still earning their write cost without breaking the production query plan.

A table that started with two indexes five years ago now has eleven. Every developer who fixed a slow query added one; nobody ever removed one. Each index costs disk, costs write throughput on every INSERT and UPDATE, and forces the optimizer to consider more plans on every SELECT. An index that was right in 2022 against 200,000 rows can be wrong in 2026 against 40 million — the optimizer may pick it, walk a huge B-tree, and end up slower than a full scan. An index review is one of the highest-value, lowest-risk pieces of database hygiene — if done carefully.

What’s happening

MySQL maintains usage counters in performance_schema and sys. Every time the optimizer chooses an index, it bumps a counter. Indexes not chosen since the last restart are candidates for removal — but only candidates. Some only get used during quarterly reports. Some are required for foreign key checks even when no query names them. Dropping an index the optimizer was relying on as a fallback can push it onto a much worse plan for a hot query the next morning. The trap is removing a “free” index that turns out to be load-bearing.

How to verify

Find indexes that have not been touched since the last server restart:

SELECT object_schema, object_name, index_name
FROM sys.schema_unused_indexes
WHERE object_schema NOT IN ('mysql', 'sys', 'performance_schema');

Cross-check against actual IO statistics — an index might be unused for SELECTs but heavily written to:

SELECT object_schema, object_name, index_name,
       count_read, count_write, count_fetch
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE object_schema = 'your_db'
  AND index_name IS NOT NULL
ORDER BY count_read ASC;

An index with zero reads and millions of writes is pure tax. List all indexes on a candidate table with their cardinality, which tells you how selective they are:

SELECT index_name, column_name, cardinality, seq_in_index
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = 'your_db' AND table_name = 'your_table'
ORDER BY index_name, seq_in_index;

Before removing anything, capture the EXPLAIN of the top ten queries hitting that table — you need a baseline if the optimizer picks a worse plan after the drop.

The fix

Don’t run DROP INDEX against a hot production table in business hours. Use pt-online-schema-change to drop the index without taking a write lock:

pt-online-schema-change --alter "DROP INDEX idx_old_unused" \
  D=your_db,t=your_table --execute

It builds a shadow table, copies rows in chunks, syncs writes via triggers, and swaps atomically. To add an index, the inverse: build it shadow-side, swap when ready. After each change, re-run the captured EXPLAINs and watch the slow query log for a day. If a query that was on an index merge plan suddenly does a full scan, you removed something that wasn’t truly unused — put it back. Adding an index can also degrade a plan: a new low-selectivity index can lure the optimizer away from a better composite it was using.

Production index reviews pay for themselves in write throughput and storage. Stack Harbor’s Cloud PaaS team does these as a scheduled engagement — request a review.