这将为您提供查询计划:
SELECT cplan.usecounts,
cplan.objtype,
qtext.text,
qplan.query_plan
FROM sys.dm_exec_cached_plans AS cplan
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS qtext
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qplan
WHERE qtext.text LIKE '%UniqueName%'
ORDER BY cplan.usecounts DESC;
在这里你可以列出那些超时的:
WITH XMLNAMESPACES(DEFAULT N'http://schemas.microsoft.com/sqlserver/2004/07/showplan'), QueryPlans
AS (
SELECT RelOp.pln.value(N'@StatementOptmEarlyAbortReason', N'varchar(50)') AS TerminationReason,
RelOp.pln.value(N'@StatementOptmLevel', N'varchar(50)') AS OptimizationLevel,
--dest.text,
SUBSTRING(dest.text, (deqs.statement_start_offset / 2) + 1,
(deqs.statement_end_offset - deqs.statement_start_offset)
/ 2 + 1) AS StatementText,
deqp.query_plan,
deqp.dbid,
deqs.execution_count,
deqs.total_elapsed_time,
deqs.total_logical_reads,
deqs.total_logical_writes
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp
CROSS APPLY deqp.query_plan.nodes(N'//StmtSimple') RelOp (pln)
WHERE deqs.statement_end_offset > -1
)
SELECT DB_NAME(qp.dbid),
*
FROM QueryPlans AS qp
WHERE qp.TerminationReason = 'Timeout'
ORDER BY qp.execution_count DESC ;