且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 查找未使用的索引

更新时间:2022-08-20 09:05:58

Simple but accurate following script will give you list of all the indexes in the database which are unused. If indexes are not used they should be dropped as Indexes reduces the performance for INSERT/UPDATE statement. Indexes are only useful when used with SELECT statement.

Script to find unused Indexes.

USE AdventureWorks
GO
DECLARE @dbid INT
SELECT 
@dbid DB_ID(DB_NAME())
SELECT OBJECTNAME OBJECT_NAME(I.OBJECT_ID),
INDEXNAME I.NAME,
I.INDEX_ID
FROM SYS.INDEXES I
JOIN SYS.OBJECTS O
ON I.OBJECT_ID O.OBJECT_ID
WHERE OBJECTPROPERTY(O.OBJECT_ID,'IsUserTable'1
AND I.INDEX_ID NOT IN (
SELECT S.INDEX_ID
FROM SYS.DM_DB_INDEX_USAGE_STATS S
WHERE S.OBJECT_ID I.OBJECT_ID
AND I.INDEX_ID S.INDEX_ID
AND DATABASE_ID @dbid)
ORDER BY OBJECTNAME,
I.INDEX_ID,
INDEXNAME ASC
GO

Note: This is only for SQL Server 2005, it will not run for previous version of SQL Server.



    本文转自 Fanr_Zh 博客园博客,原文链接:http://www.cnblogs.com/Amaranthus/archive/2011/04/18/2019799.html,如需转载请自行联系原作者