开发者

how to tell if an index is ever used [duplicate]

开发者 https://www.devze.com 2023-02-14 10:38 出处:网络
This question already has answers here: SQL Server - how to determine if indexes aren't being used?
This question already has answers here: SQL Server - how to determine if indexes aren't being used? 开发者_StackOverflow (4 answers) Closed 6 years ago.

I have some old tables with, what I think, are nearly worthless indexes. How can I easily be sure that are not ever being used before I drop them.


Based on Joe's answer I came up with this:

SELECT
      row_number() over(order by user_seeks,user_lookups,user_scans),
      [Database] = d.name,
      [Schema]= s.name,
      [Table]= o.name,
      [Index]= x.name,
      [Scans] = user_scans,
      [Seeks] = user_seeks,
      [Lookups] = user_lookups,
      [Last Scan] = last_user_scan,
      [System Scans] = system_scans
FROM  sys.dm_db_index_usage_stats u
INNER JOIN sys.sysdatabases d on u.database_id = d.dbid
INNER JOIN sys.sysindexes x on u.object_id = x.id  and u.index_id = x.indid
INNER JOIN sys.objects o on u.object_id = o.object_id
INNER JOIN sys.schemas s on s.schema_id = o.schema_id
where  x.name is not null
order by 1 desc


Take a look at the sys.dm_db_index_usage_stats DMV. Just be careful to keep in mind that these counters are reset every time SQL Server is restarted.


Check if the user seeks of the index are increasing with every select on the table u r using unless the server is restarted this will give you a clear idea which index is being used or not.

SELECT DISTINCT OBJECT_NAME(sis.OBJECT_ID) TableName, si.name AS IndexName, sc.Name AS ColumnName,
sic.Index_ID, sis.user_seeks, sis.user_scans, sis.user_lookups, sis.user_updates
FROM sys.dm_db_index_usage_stats sis
INNER JOIN sys.indexes si ON sis.OBJECT_ID = si.OBJECT_ID AND sis.Index_ID = si.Index_ID
INNER JOIN sys.index_columns sic ON sis.OBJECT_ID = sic.OBJECT_ID AND sic.Index_ID = si.Index_ID
INNER JOIN sys.columns sc ON sis.OBJECT_ID = sc.OBJECT_ID AND sic.Column_ID = sc.Column_ID
WHERE sis.Database_ID = DB_ID('database_name') AND sis.OBJECT_ID = OBJECT_ID('table_name');
GO


Check out this section of this article by Ian Stirk in 2007. Very nice set of tools, including finding your unused indexes.

0

精彩评论

暂无评论...
验证码 换一张
取 消