Skip to content

Instantly share code, notes, and snippets.

@PiotrFerenc
Created April 14, 2025 07:48
Show Gist options
  • Save PiotrFerenc/fbd8409d4f551d91ff7fc54247b13102 to your computer and use it in GitHub Desktop.
Save PiotrFerenc/fbd8409d4f551d91ff7fc54247b13102 to your computer and use it in GitHub Desktop.
SELECT
OBJECT_NAME(i.object_id) AS TableName,
i.name AS IndexName,
i.index_id,
i.type_desc,
i.is_unique,
i.is_primary_key,
i.is_disabled
FROM
sys.indexes i
LEFT JOIN
sys.dm_db_index_usage_stats s
ON i.object_id = s.object_id AND i.index_id = s.index_id
AND s.database_id = DB_ID()
WHERE
OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
AND i.type_desc <> 'HEAP'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
AND (
s.user_seeks IS NULL AND
s.user_scans IS NULL AND
s.user_lookups IS NULL AND
s.user_updates IS NULL
)
ORDER BY
TableName, IndexName;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment