Skip to content

Indexes never used

Indexes with no reads since the server started, from performance_schema. Restart resets the counters — trust it after a few days of uptime.

MySQL · SQL
SELECT CONCAT(object_schema, '.', object_name) AS table_name,       index_nameFROM performance_schema.table_io_waits_summary_by_index_usageWHERE index_name IS NOT NULL  AND index_name <> 'PRIMARY'  AND count_star = 0  AND object_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')ORDER BY object_schema, object_name

Needs something on the server: performance_schema must be ON.

This query is part of the "MySQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

This query lists the indexes that have not been read since the server started. It reads performance_schema.table_io_waits_summary_by_index_usage and keeps rows with count_star equal to zero. The PRIMARY key and the system schemas are excluded.

Every index costs disk, memory in the buffer pool and time on every INSERT, UPDATE and DELETE. An index nobody reads is pure cost. Run this before you drop anything and when writes are slower than they should be.

How to read each column

  • table_name: schema and table that own the index.
  • index_name: the index that had zero uses.

Zero uses means no SELECT, INSERT, UPDATE or DELETE touched that index through the table I/O instrumentation since the counters started.

How much to trust the result

Uptime decides. The counters reset on every restart. These are rules of thumb:

  • Less than a few days of uptime: do not act on the list.
  • A full business cycle (a month, if you have month-end reports) with the server up: the list is reliable.
  • A UNIQUE index that shows up may still be doing a job: it enforces a constraint on every write even if no query reads it. Keep it.
  • Foreign key indexes can show zero reads and still be needed for the constraint checks.

What to do about it

Do not drop straight away. In MySQL 8.0 make the index invisible first. The optimizer ignores it, but MySQL keeps maintaining it, so you can bring it back instantly:

ALTER TABLE shop.orders ALTER INDEX idx_old_status INVISIBLE;

Watch for slow queries for a full cycle. If nothing complains, drop it. If something does, make it visible again:

ALTER TABLE shop.orders ALTER INDEX idx_old_status VISIBLE;

-- After a quiet cycle:
ALTER TABLE shop.orders DROP INDEX idx_old_status;

Save the CREATE INDEX statement before dropping, so you can recreate it. On a very large table rebuilding the index can take a long time.

Limits of this query

  • The performance_schema must be ON and the table I/O instrumentation enabled. Both are the defaults on MySQL 5.6 and later.
  • MySQL 5.7 and later ship the sys schema with a view called sys.schema_unused_indexes. It reads the same Performance Schema table and skips the primary key. Unique indexes still show up in both lists, and they enforce a rule even when no query reads them, so leave them out of any cleanup.
  • Indexes used only by a rare job, like a yearly report, look unused until that job runs.
  • On a replica, the counters reflect only what runs on the replica.
  • The account needs SELECT on performance_schema. MariaDB has the table, but check that its performance_schema is on.

Review it monthly in DataRunner

A monthly schedule fits this one. The server then collects a long enough window of usage, and you get the list by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets. Add the option to skip a run with no rows if you only want to hear about it when there is a candidate.

Frequently asked questions

How do I find unused indexes in MySQL?
Query performance_schema.table_io_waits_summary_by_index_usage and keep indexes where count_star is 0. On MySQL 5.7 and later you can also read sys.schema_unused_indexes. Use it only after the server has been up for a full business cycle.
Is it safe to drop an index that shows zero reads?
Not right away. A unique or foreign key index can enforce rules without being read, and a rare job may use it later. In MySQL 8.0 make it invisible first and drop it after a quiet cycle.
What is an invisible index in MySQL?
It is an index the optimizer ignores while MySQL keeps updating it. If a query gets slower you make it visible again with ALTER INDEX and nothing is rebuilt. It is available from MySQL 8.0.
Do restarts reset the unused index list?
Yes. The performance_schema counters start at zero when the server starts, so an index looks unused after every restart until a query reads it.