Skip to content

Instantly share code, notes, and snippets.

@richardbasile
Last active March 16, 2018 16:21
Show Gist options
  • Save richardbasile/b6ecabb6b9d3c4035e9db30a782f2a53 to your computer and use it in GitHub Desktop.
Save richardbasile/b6ecabb6b9d3c4035e9db30a782f2a53 to your computer and use it in GitHub Desktop.
SQL Server - Index Usage
select t.name AS table_name
, i.name AS index_name
, i.type_desc AS index_type
, i.is_unique
, i.is_primary_key
, s.user_seeks
, s.user_scans
, s.user_lookups
, s.user_updates
, (SELECT MAX(ts) FROM (
VALUES (s.last_user_seek), (s.last_user_scan), (s.last_user_lookup)
) ts(ts) ) AS [LastUsed]
, (SELECT SUM(p.[used_page_count]) * 8
FROM sys.dm_db_partition_stats p
WHERE p.object_id = i.object_id AND p.index_id = i.index_id
) AS [IndexSizeKB]
FROM sys.indexes i
JOIN sys.tables t ON t.object_id = i.object_id
LEFT JOIN sys.dm_db_index_usage_stats s
ON i.object_id = s.object_id AND i.index_id = s.index_id
WHERE i.name IS NOT NULL
ORDER BY user_seeks + user_scans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment