Last active
March 16, 2018 16:21
-
-
Save richardbasile/b6ecabb6b9d3c4035e9db30a782f2a53 to your computer and use it in GitHub Desktop.
SQL Server - Index Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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