Performance: Query the sqlserver plan cache to find queries that use a particular index

Part of keeping a SQL Server database running smoothly is to keep
indexes well tuned. As database schemas or usage change, indexes that
were once important may fall out of use. Eliminating unnecessary
indexes helps speed up inserts/updates/deletes, and it saves disk space.

Starting in SQL Server 2005, Microsoft introduced some very helpful
dynamic management views (dmv), including one that tracks index usage
. It records the number of seeks and scans each
index has received since the database came online. By looking at
indexes with very low seek/scan rates, you can quickly find indexes that
are candidates for removal.

The situation is murkier when you find indexes that you believe are bad,
but the dmv’s show they are being accessed. For example, you might have
an index that includes a column that is not regularly queried with, and
you want to remove it or replace it with a more helpful column. Most
likely, the index is being used because its other columns still make it
a good choice for certain queries, but there is always the possibility
that some obscure query needs that data and will suffer a major
performance penalty if the column is removed.

The only way to assess the risk is to go and find the queries that are
making use of the index and see if they actually need the column you
think you can eliminate. The question is, how do you find them?

Back in the SQL Server 2000 days, the profiler had an option that would
like you query against the text inside of query execution plans. You
could set it up with a filter that matched the name of your index, and
then start capturing. This worked pretty well, but you had to be
confident that you were capturing enough data to really find all of the
examples of usage.

With SQL Server 2008, this profiling trick doesn’t work anymore, but
there is another way. SQL Server maintains a plan cache for queries it
has executed. Using a couple of dmv’s, you can search for query plans
that use your index and then look at them to see if they would be
affected by your proposed change.

Below is how to do it. There are a couple of flavors of this query out
there on the internet. The one below is adapted from a couple of
sources that I can no longer pinpoint, but if you want more examples of
these types of queries, check out Jason Strate’s blog.

DECLARE @IndexName sysname = 'Idx_SomeTableName_SomeIndex';

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' as p)

SELECT 

     DB_NAME(dbid) + '.' + OBJECT_SCHEMA_NAME(objectid, dbid) + '.' + OBJECT_NAME(objectid, dbid) AS database_object
     ,iobj.value('@Index','sysname') AS IndexName
     ,cp.plan_handle
     ,iobj.query('.') AS IndexUsage
     ,qp.query_plan
     ,cp.plan_handle

 FROM sys.dm_exec_cached_plans cp 
 CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
 CROSS APPLY query_plan.nodes('//p:RelOp') IndexScan(scan)
 CROSS APPLY scan.nodes('//p:Object') as IndexObject(iobj)
 WHERE iobj.value('@Index','nvarchar(max)') = QUOTENAME(@IndexName,'[')

This query will then scan the procedure cache and return query plans
that contained the index in question:

Clicking on one of the plans will pull up the plan view, along with the
sql statements. Now you just need to find the index and see how it is
being used.

If you none of the plans would be affected by your proposed changes,
then you can be more confident that your index modification will not
adversely impact performance.

Of course, nothing is guaranteed. These are just the plans in cache,
and it is possible that a rare query will come along need your data. So
as always, proceed cautiously.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

3 Responses to Performance: Query the sqlserver plan cache to find queries that use a particular index

  1. Nij says:

    This is brilliant! Are you aware of any way to strip out the duplicate rows, and also (perhaps more importantly) remove the rows that show the index being updated or inserted?It would be ideal if I could just search for the ‘real’ uses of the index (the seeks and scans), not the cost of that use (inserts, updates, deletes).

  2. Dan says:

    Very cool !!!ThanksDanPhttp://madebysql.blogspot.com/

  3. Chaitanya says:

    Dear sir, I am a beginner level, have SQL Server 2005 and I am getting error. Could you please share script for 2005 version.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s