Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Find all queries that use an index?

Author  Topic 

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2011-10-11 : 15:31:17
Is there a query that will tell me all of the query plans that make use of an index?

Using the "index usage" report I see an index that has lots of "user seeks" but also lots of "user scans". This tells me that the index MAY need to include more columns or isn't quite setup ideally.

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-10-11 : 16:45:04
No.

What you can do, with lots of xquery, is see all query plans in cache that use a particular index, but that's just the plans that are currently in cache.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2011-10-12 : 18:29:02
Thanks for the response.

Is it just me or would being able to readily pull this information greatly improve the management of indexes. If you could see all of the queries using an index (and the usage counts) you could more easily manage the altering of index fields (ordering and inclusion/exclusion of fields) and identify indexes to merge.

There must be something out there or is there a better way to approach managing indexes? Using the various DMV queries and reports really helps identify needed indexes and unused indexes but I haven't found anything to advise on tweaking existing indexes or merging duplicate (or near duplicate) indexes.
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2011-10-18 : 12:23:16
For those who are curious. I found this query that sort of achieves what I want. It isn't exactly what I want but it is something...


SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
WITH XMLNAMESPACES
(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT TOP 100--ecp.plan_handle,
DENSE_RANK() OVER ( ORDER BY ecp.plan_handle ) AS ArbitraryPlanNumber ,
n.value('(@StatementSubTreeCost)[1]', 'VARCHAR(128)')
* ISNULL(n.value('(./QueryPlan/MissingIndexes/MissingIndexGroup/@Impact)[1]','float'), 0)
* ecp.usecounts AS Improvement ,
query_plan AS CompleteQueryPlan ,
n.value('(@StatementId)[1]', 'float') AS StatementID ,
n.value('(@StatementText)[1]', 'VARCHAR(4000)') AS StatementText ,
n.value('(@StatementSubTreeCost)[1]', 'VARCHAR(128)') AS StatementSubTreeCost ,
n.query('./QueryPlan/MissingIndexes') MissingIndex ,
n.value('(./QueryPlan/MissingIndexes/MissingIndexGroup/@Impact)[1]','float') IndexImpact ,
ecp.usecounts
FROM sys.dm_exec_cached_plans AS ecp
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS eqp
CROSS APPLY query_plan.nodes('/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple')
AS qn ( n )
WHERE n.value('(./QueryPlan/MissingIndexes/MissingIndexGroup/@Impact)[1]','float') IS NOT NULL AND
ecp.usecounts > 100 AND
eqp.query_plan.exist('declare default element namespace "http://schemas.microsoft.com/sqlserver/2004/07/showplan";
/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/QueryPlan/MissingIndexes') = 1
ORDER BY Improvement DESC
Go to Top of Page
   

- Advertisement -