Skip to content

Instantly share code, notes, and snippets.

@paschott
Last active July 19, 2021 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paschott/1d0eb1a5b80ab332366514bd9c6761e8 to your computer and use it in GitHub Desktop.
Save paschott/1d0eb1a5b80ab332366514bd9c6761e8 to your computer and use it in GitHub Desktop.
SELECT
objects.name AS Table_name,
indexes.name AS Index_name,
dm_db_index_usage_stats.user_seeks,
dm_db_index_usage_stats.user_scans,
dm_db_index_usage_stats.user_updates
FROM
sys.dm_db_index_usage_stats
INNER JOIN sys.objects ON dm_db_index_usage_stats.OBJECT_ID = objects.OBJECT_ID
INNER JOIN sys.indexes ON indexes.index_id = dm_db_index_usage_stats.index_id AND dm_db_index_usage_stats.OBJECT_ID = indexes.OBJECT_ID
WHERE
--indexes.is_primary_key = 0 --This line excludes primary key constarint
--AND
--indexes. is_unique = 0 --This line excludes unique key constarint
--AND
dm_db_index_usage_stats.user_updates <> 0 -- This line excludes indexes SQL Server hasn’t done any work with
AND
dm_db_index_usage_stats. user_lookups = 0
AND
dm_db_index_usage_stats.user_seeks = 0
AND
dm_db_index_usage_stats.user_scans = 0
ORDER BY
dm_db_index_usage_stats.user_updates DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment