brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: MySQL's sys.schema_unused_indexes view lists indexes nothing has used

Mysql
~/tips/tip-mysql-sys-unused-indexes

SELECT * FROM sys.schema_unused_indexes

Finds candidates for removal that only slow down writes.

Indexes pile up. Every one costs a bit on each insert and update, and some were added for a query that no longer exists.

The built in sys schema keeps a view of those with no recorded use:

SELECT * FROM sys.schema_unused_indexes
WHERE object_schema = 'shopdb';

Each row names the schema, table and index. Treat the result as a list of suspects, not a list of things to delete.

The reason: the counters only cover the time since the server started. The manual notes that the view is most useful after the server has run long enough for its workload to be representative. A monthly report or a yearly cleanup job may be the only user of an index, and shortly after a restart nearly everything looks unused. Check the uptime first:

SHOW GLOBAL STATUS LIKE 'Uptime';

Replicas count separately, so an index idle on the primary might be busy on a read replica. Look at both.

Before dropping one, I’d make it invisible for a while:

ALTER TABLE orders ALTER INDEX idx_old INVISIBLE;

The optimizer then ignores it while it stays maintained, and VISIBLE brings it back instantly if something slows down. When weeks go by without complaints, drop it, and keep the CREATE INDEX statement somewhere in case you were wrong. Unique indexes enforce a rule even when no query reads them, so leave those unless the rule itself is gone.

Further reading: sys.schema_unused_indexes, Invisible indexes

More Mysql tips FOR UPDATE SKIP LOCKED; mysql_config_editor set --login-path=shop \ SET PERSIST max_connections = 300; ALTER TABLE orders ADD COLUMN note VARCHAR(100), ALGORITHM=INSTANT; ALTER USER 'appuser'@'192.0.2.10' All Mysql tips (8) All topics

Link