All we need is an easy explanation of the problem, so here it is.
Is there a query I can run to search the plan cache for all query plans using a given index?
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
I’d recommend using sys.dm_exec_text_query_plan
instead of sys.dm_exec_query_plan
. Using sys.dm_exec_query_plan
you will lose stored procedures with deferred compilation, long-running stored procedures (in case the SP has not finished but its statement which is using the given index has already done), and multi-statement UDF.
Additionally, you should add hints to avoid possible negative influence on your working environment.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
DECLARE @IndexName AS NVARCHAR(128) = N'[YourIndexName]';
;WITH XMLNAMESPACES
(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT
DB_NAME(x.dbid),
x.ObjectName,
stmt.value('(@StatementText)[1]', 'varchar(max)') AS SQL_Text,
obj.value('(@Database)[1]', 'varchar(128)') AS DatabaseName,
obj.value('(@Schema)[1]', 'varchar(128)') AS SchemaName,
obj.value('(@Table)[1]', 'varchar(128)') AS TableName,
obj.value('(@Index)[1]', 'varchar(128)') AS IndexName,
obj.value('(@IndexKind)[1]', 'varchar(128)') AS IndexKind,
x.plan_handle,
x.query_plan
FROM (
SELECT try_CONVERT(XML, qp.query_plan) AS query_plan, cp.plan_handle, OBJECT_NAME(qp.objectid, qp.dbid) AS ObjectName, qp.objectid, qp.dbid
FROM sys.dm_exec_cached_plans AS cp
JOIN sys.dm_exec_query_stats s ON s.plan_handle = cp.plan_handle
cross apply sys.dm_exec_text_query_plan (s.plan_handle, s.statement_start_offset, s.statement_end_offset) AS qp
) x
CROSS APPLY query_plan.nodes('/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple') AS batch(stmt)
CROSS APPLY stmt.nodes('.//IndexScan/Object[@Index=sql:variable("@IndexName")]') AS idx(obj)
OPTION(MAXDOP 1, RECOMPILE);
Method 2
As well as a simple text search, you can also use an XQuery filter, which may be faster or slower.
DECLARE @idx sysname = N'[IX_YourIndexName]';
WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT
qp.query_plan,
qs.execution_count,
st.text
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
INNER JOIN sys.dm_exec_query_stats qs ON qs.plan_handle = cp.plan_handle
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE qp.query_plan.exist('//Object[@Index = sql:variable("@idx")]') = 1;
Or if you want just the query plans:
DECLARE @idx sysname = N'[IX_YourIndexName]';
WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT
qp.query_plan
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
WHERE qp.query_plan.exist('//Object[@Index = sql:variable("@idx")]') = 1;
You can also add filters on @Database
and @Schema
if necessary.
If you are copying the index name out of system views, make sure to use QUOTENAME
otherwise it won’t match.
Method 3
try this:
SELECT
dm_exec_query_plan.query_plan,
usecounts AS execution_count,
dm_exec_sql_text.text
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_query_plan(plan_handle)
INNER JOIN sys.dm_exec_query_stats
ON dm_exec_query_stats.plan_handle = dm_exec_cached_plans.plan_handle
CROSS APPLY sys.dm_exec_sql_text(dm_exec_query_stats.plan_handle)
WHERE CAST(dm_exec_query_plan.query_plan AS NVARCHAR(MAX)) LIKE '%index_you_are_looking_for%'
- beware of indexes with similar names like this: named
CIX_Customer_CreateDate
andIX_CustomerAddress
will return from a search forLIKE '%IX_Customer%'
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0