Published on

September 12, 2008

How to Determine if an Index is Being Used in SQL Server

One common question that often arises is how to find out if any index is being used in a SQL Server database. If a database has many indexes and not all of them are being used, it can negatively impact performance. While having a higher number of indexes can reduce INSERT, UPDATE, and DELETE operations, it can increase SELECT operations. Therefore, it is recommended to drop any unused indexes from a table to improve performance.

Before dropping an index, it is important to check if the index is being used or not. In SQL Server 2005 and later editions, we can use Dynamic Management Views (DMV) to retrieve the necessary information. Let’s run a quick script to determine if an index is being used.

USE AdventureWorks
GO

SELECT DISTINCT OBJECT_NAME(sis.OBJECT_ID) AS 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('AdventureWorks') AND sis.OBJECT_ID = OBJECT_ID('HumanResources.Employee');
GO

In the above script, we are querying the sys.dm_db_index_usage_stats DMV to retrieve information about index usage. We are specifically looking at the Employee table in the AdventureWorks database. The result of this query will provide us with details about the indexes used, such as the index name, column name, and usage statistics.

To test if an index is being used, we can run SELECT statements on the Employee table using different indexes. For example:

USE AdventureWorks
GO

SELECT * FROM HumanResources.Employee WITH (INDEX = 1) WHERE EmployeeID = 1
GO

SELECT * FROM HumanResources.Employee WITH (INDEX = 2) WHERE LoginID = 'adventure-works\guy1'
GO

In the above example, we are running two SELECT statements on the Employee table, specifying different indexes using the “WITH (INDEX = Number)” syntax. Both queries will return the same result, but the index usage will be recorded in the sys.dm_db_index_usage_stats DMV.

By comparing the initial query results with the results after running the SELECT statements, we can determine if the indexes are being used. If the “user_seeks” column in the sys.dm_db_index_usage_stats DMV is incremented, it indicates that the index is being used.

It is important to note that the sys.dm_db_index_usage_stats DMV stores all the usage information since the SQL Server service was last restarted. If the service is restarted, the sys.dm_db_index_usage_stats DMV is reset to zero. Therefore, it is recommended to regularly monitor the index usage and drop any indexes that are not being used frequently.

However, before dropping an index, it is crucial to test the performance after its removal. If the performance worsens, the index should be added back and further analysis should be conducted.

By regularly monitoring and managing index usage, you can optimize the performance of your SQL Server database and ensure that only necessary indexes are maintained.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.