SELECT TOP 25 OBJECT_SCHEMA_NAME(i.object_id) + '.' + OBJECT_NAME(i.object_id) AS table_name, i.name AS index_name, u.user_updates AS writes, u.user_seeks + u.user_scans + u.user_lookups AS readsFROM sys.indexes iJOIN sys.dm_db_index_usage_stats u ON u.object_id = i.object_id AND u.index_id = i.index_id AND u.database_id = DB_ID()WHERE i.type_desc = 'NONCLUSTERED' AND i.is_primary_key = 0 AND i.is_unique = 0 AND u.user_seeks + u.user_scans + u.user_lookups = 0ORDER BY u.user_updates DESCThis query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What the query shows
An index that nobody reads still has to be updated on every write. This query reads sys.dm_db_index_usage_stats for the current database and lists nonclustered indexes that were written to but never used by a seek, a scan or a lookup. It skips primary keys and unique indexes, and returns the top 25 by number of writes.
Run it when inserts and updates are slower than they should be, when the database is growing fast, or during a tidy-up of old schemas.
How to read each column
- table_name: schema and table the index belongs to.
- index_name: the index candidate.
- writes: user_updates, the number of times queries had to maintain the index (insert, update or delete).
- reads: user_seeks plus user_scans plus user_lookups. The query only returns rows where this is 0.
Healthy and worrying results
An empty list is healthy. A row with thousands or millions of writes and zero reads costs you CPU, log and disk on every change. A row with few writes costs little, so it is not urgent even if nobody reads it.
How long the server has been up matters most. As a rule of thumb, trust the zero only after the server has run through a full business cycle, such as a month-end close. An index used only by a quarterly report looks unused until the quarter ends.
What to do about it
Before dropping, save the definition so you can recreate the index, and confirm the server uptime.
SELECT sqlserver_start_time FROM sys.dm_os_sys_info;The cautious route is to disable the index first. A disabled index keeps its definition, stops being maintained and is not used by queries. If something breaks, a rebuild brings it back. After a few weeks without complaints, drop it.
ALTER INDEX IX_Orders_Status ON dbo.Orders DISABLE;
-- to bring it back:
ALTER INDEX IX_Orders_Status ON dbo.Orders REBUILD;
-- after weeks without problems:
DROP INDEX IX_Orders_Status ON dbo.Orders;Limits to know
- The counters start at zero when the service restarts, so a recent restart makes many useful indexes look unused.
- An index that was never touched since the restart has no row in the DMV and does not appear here at all.
- Primary keys and unique indexes are excluded on purpose. They may enforce a rule even when nobody reads them.
- Heaps and clustered indexes are out of scope, because the filter is NONCLUSTERED.
- The DMV needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). In Azure SQL, counters can also reset on failover or scaling.
Run it on a schedule in DataRunner
Run this query once a month in DataRunner and send the result to the person who owns the schema, by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets, as CSV, XLSX, PDF or HTML. An index that shows up in several months in a row, after restarts, is a much better candidate than one seen once.