// SQL snippet

Find indexes never scanned in SQL Server

An index that is written on every insert but never read costs you something and returns nothing. sys.dm_db_index_usage_stats finds them.

Main statement
SQL Server

The task

SQL Server counts user seeks, scans and lookups per index in sys.dm_db_index_usage_stats, alongside user_updates, the number of write operations that had to maintain the index. An index with zero reads and thousands of updates is paid for on every INSERT, every DELETE and every UPDATE that touches its columns, and no query has used it since the counters started.

The snippet

Written for SQL Server.

SQL Server
SELECT OBJECT_SCHEMA_NAME(i.object_id) AS schema_name,
       OBJECT_NAME(i.object_id) AS table_name,
       i.name AS index_name, i.type_desc,
       us.user_seeks, us.user_scans, us.user_lookups,
       us.user_updates,
       us.last_user_update
FROM sys.indexes i
JOIN sys.dm_db_index_usage_stats us
  ON us.object_id = i.object_id AND us.index_id = i.index_id AND us.database_id = DB_ID()
WHERE i.index_id > 1
  AND i.is_unique = 0
  AND us.user_seeks = 0 AND us.user_scans = 0 AND us.user_lookups = 0
ORDER BY us.user_updates DESC;

它的運作方式

sys.dm_db_index_usage_stats is instance-wide, so the join filters on database_id = DB_ID() to stay in the current database. A row appears only once an index has been touched, which is fine here: an index with no row at all has not been written either. index_id > 1 skips the heap (0) and the clustered index (1), which hold the table's data and cannot be dropped as a tuning step. is_unique = 0 skips primary keys, unique constraints and unique indexes, because they enforce rules. Ordering by user_updates puts the most expensive dead weight first.

Variations

Include index size in the result

SELECT OBJECT_SCHEMA_NAME(i.object_id) AS schema_name,
       OBJECT_NAME(i.object_id) AS table_name, i.name AS index_name,
       us.user_updates,
       CAST(SUM(ps.used_page_count) * 8 / 1024.0 AS DECIMAL(12,1)) AS size_mb
FROM sys.indexes i
JOIN sys.dm_db_index_usage_stats us
  ON us.object_id = i.object_id AND us.index_id = i.index_id AND us.database_id = DB_ID()
JOIN sys.dm_db_partition_stats ps
  ON ps.object_id = i.object_id AND ps.index_id = i.index_id
WHERE i.index_id > 1 AND i.is_unique = 0
  AND us.user_seeks = 0 AND us.user_scans = 0 AND us.user_lookups = 0
GROUP BY i.object_id, i.name, us.user_updates
ORDER BY us.user_updates DESC;

Questions that come up

When are the counters reset?

On instance restart, and when the database is detached, taken offline or closed by AUTO_CLOSE. sqlserver_start_time in sys.dm_os_sys_info tells you how long the instance has been counting. On an availability group, each replica keeps its own counters, so an index used only by reports on a readable secondary looks unused on the primary.

The query says an index is never used. Can I drop it right away?

Wait for a window that includes month-end and other rare jobs, and check for queries that name the index in a WITH (INDEX(...)) hint, because those fail once it is gone. ALTER INDEX ... DISABLE is a softer first step: it removes the index's data and its write cost but keeps the definition, and ALTER INDEX ... REBUILD brings it back.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.