Skip to content

Instantly share code, notes, and snippets.

@Jalalx
Last active June 20, 2020 17:55
Show Gist options
  • Save Jalalx/1b17706492ac9d924121868bd0960d17 to your computer and use it in GitHub Desktop.
Save Jalalx/1b17706492ac9d924121868bd0960d17 to your computer and use it in GitHub Desktop.
Query for determining heaviest hit tables and indexes. More info: https://goo.gl/Zxx2U8
--get most used tables
SELECT
db_name(ius.database_id) AS DatabaseName,
t.NAME AS TableName,
SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) AS NbrTimesAccessed
FROM sys.dm_db_index_usage_stats ius
INNER JOIN sys.tables t ON t.OBJECT_ID = ius.object_id
WHERE database_id = DB_ID('MyDb')
GROUP BY database_id, t.name
ORDER BY SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) DESC
--get most used indexes
SELECT
db_name(ius.database_id) AS DatabaseName,
t.NAME AS TableName,
i.NAME AS IndexName,
i.type_desc AS IndexType,
ius.user_seeks + ius.user_scans + ius.user_lookups AS NbrTimesAccessed
FROM sys.dm_db_index_usage_stats ius
INNER JOIN sys.indexes i ON i.OBJECT_ID = ius.OBJECT_ID AND i.index_id = ius.index_id
INNER JOIN sys.tables t ON t.OBJECT_ID = i.object_id
WHERE database_id = DB_ID('MyDb')
ORDER BY ius.user_seeks + ius.user_scans + ius.user_lookups DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment