|
| 1 | +--queries with biggest compile time |
| 2 | + |
| 3 | +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; |
| 4 | +WITH XMLNAMESPACES |
| 5 | +(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan') |
| 6 | +SELECT TOP 10 |
| 7 | +CompileTime_ms, |
| 8 | +CompileCPU_ms, |
| 9 | +CompileMemory_KB, |
| 10 | +qs.execution_count, |
| 11 | +qs.total_elapsed_time/1000 AS duration_ms, |
| 12 | +qs.total_worker_time/1000 as cputime_ms, |
| 13 | +(qs.total_elapsed_time/qs.execution_count)/1000 AS avg_duration_ms, |
| 14 | +(qs.total_worker_time/qs.execution_count)/1000 AS avg_cputime_ms, |
| 15 | +qs.max_elapsed_time/1000 AS max_duration_ms, |
| 16 | +qs.max_worker_time/1000 AS max_cputime_ms, |
| 17 | +SUBSTRING(st.text, (qs.statement_start_offset / 2) + 1, |
| 18 | +(CASE qs.statement_end_offset |
| 19 | +WHEN -1 THEN DATALENGTH(st.text) |
| 20 | +ELSE qs.statement_end_offset |
| 21 | +END - qs.statement_start_offset) / 2 + 1) AS StmtText, |
| 22 | +query_hash, |
| 23 | +query_plan_hash |
| 24 | +FROM |
| 25 | +( |
| 26 | +SELECT |
| 27 | +c.value('xs:hexBinary(substring((@QueryHash)[1],3))', 'varbinary(max)') AS QueryHash, |
| 28 | +c.value('xs:hexBinary(substring((@QueryPlanHash)[1],3))', 'varbinary(max)') AS QueryPlanHash, |
| 29 | +c.value('(QueryPlan/@CompileTime)[1]', 'int') AS CompileTime_ms, |
| 30 | +c.value('(QueryPlan/@CompileCPU)[1]', 'int') AS CompileCPU_ms, |
| 31 | +c.value('(QueryPlan/@CompileMemory)[1]', 'int') AS CompileMemory_KB, |
| 32 | +qp.query_plan |
| 33 | +FROM sys.dm_exec_cached_plans AS cp |
| 34 | +CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) AS qp |
| 35 | +CROSS APPLY qp.query_plan.nodes('ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple') AS n(c) |
| 36 | +) AS tab |
| 37 | +JOIN sys.dm_exec_query_stats AS qs |
| 38 | +ON tab.QueryHash = qs.query_hash |
| 39 | +CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st |
| 40 | + ORDER BY CompileTime_ms DESC |
| 41 | +--order by execution_count desc |
| 42 | +OPTION(RECOMPILE, MAXDOP 1); |
0 commit comments