如何判断是否使用过索引

时间:2011-02-28 17:27:46

标签: sql-server sql-server-2005

我有一些旧桌子,我认为,几乎是毫无价值的索引。我怎样才能轻易确定在放弃它之前从未使用过。

4 个答案:

答案 0 :(得分:12)

根据Joe的回答,我想出了这个:

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

答案 1 :(得分:5)

看看sys.dm_db_index_usage_stats DMV。请注意,每次重新启动SQL Server时都会重置这些计数器。

答案 2 :(得分:0)

2007年Ian Stirk查看this section this article。非常好的工具集,包括查找未使用的索引。

答案 3 :(得分:0)

除非服务器重新启动,否则检查用户是否正在使用表中的每个选项来查找索引是否正在增加,这将使您清楚地知道正在使用哪个索引。

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