Files
sql-snippets/dmvBadIdx-b73c1b25-8626-469e-98a4-5a2ac1eabb64.json
2025-07-07 09:09:23 +02:00

6 lines
1.3 KiB
JSON

{
"id": "b73c1b25-8626-469e-98a4-5a2ac1eabb64",
"prefix": "dmvBadIdx",
"description": "check for bad indexes",
"body": "-- Possible Bad NC Indexes (writes > reads) (Query 52) (Bad NC Indexes)\r\nSELECT\tOBJECT_NAME(s.[object_id]) AS [Table Name]\r\n\t ,i.name AS [Index Name]\r\n\t ,i.index_id\r\n\t ,i.is_disabled\r\n\t ,i.is_hypothetical\r\n\t ,i.has_filter\r\n\t ,i.fill_factor\r\n\t ,user_updates AS [Total Writes]\r\n\t ,user_seeks + user_scans + user_lookups AS [Total Reads]\r\n\t ,user_updates - (user_seeks + user_scans + user_lookups) AS [Difference]\r\nFROM\tsys.dm_db_index_usage_stats AS s WITH (NOLOCK)\r\n\t\tINNER JOIN sys.indexes AS i WITH (NOLOCK) ON s.[object_id] = i.[object_id] AND i.index_id = s.index_id\r\nWHERE\tOBJECTPROPERTY(s.[object_id], 'IsUserTable') = 1\r\n\t\tAND s.database_id = DB_ID()\r\n\t\tAND user_updates > (user_seeks + user_scans + user_lookups)\r\n\t\tAND i.index_id > 1\r\n\t\tAND (user_seeks + user_scans + user_lookups) = 0\r\nORDER BY [Difference] DESC\r\n\t ,[Total Writes] DESC\r\n\t ,[Total Reads] ASC\r\nOPTION\t(RECOMPILE)\r\n;\r\n\r\n-- Look for indexes with high numbers of writes and zero or very low numbers of reads\r\n-- Consider your complete workload, and how long your instance has been running\r\n-- Investigate further before dropping an index!\r\n"
}