Skip to content

Commit 4621b34

Browse files
authored
Merge pull request #839 from BrentOzarULTD/sp_BlitzCache_838
Check for plans with >128 levels of nesting
2 parents 61c9198 + 68384f2 commit 4621b34

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

sp_BlitzCache.sql

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,8 +2816,7 @@ OPTION (RECOMPILE) ;
28162816
RAISERROR('Populating Warnings column', 0, 1) WITH NOWAIT;
28172817
/* Populate warnings */
28182818
UPDATE ##bou_BlitzCacheProcs
2819-
SET Warnings = CASE WHEN QueryPlan IS NULL THEN 'We couldn''t find a plan for this query. Possible reasons for this include dynamic SQL, RECOMPILE hints, and encrypted code.' ELSE
2820-
SUBSTRING(
2819+
SET Warnings = SUBSTRING(
28212820
CASE WHEN warning_no_join_predicate = 1 THEN ', No Join Predicate' ELSE '' END +
28222821
CASE WHEN compile_timeout = 1 THEN ', Compilation Timeout' ELSE '' END +
28232822
CASE WHEN compile_memory_limit_exceeded = 1 THEN ', Compile Memory Limit Exceeded' ELSE '' END +
@@ -2870,7 +2869,6 @@ SET Warnings = CASE WHEN QueryPlan IS NULL THEN 'We couldn''t find a plan for
28702869
CASE WHEN low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END +
28712870
CASE WHEN long_running_low_cpu = 1 THEN + 'Long Running With Low CPU' ELSE '' END
28722871
, 2, 200000)
2873-
END
28742872
WHERE SPID = @@SPID
28752873
OPTION (RECOMPILE) ;
28762874

@@ -2880,8 +2878,7 @@ WITH statement_warnings AS
28802878
(
28812879
SELECT DISTINCT
28822880
SqlHandle,
2883-
Warnings = CASE WHEN QueryPlan IS NULL THEN 'We couldn''t find a plan for this query. Possible reasons for this include dynamic SQL, RECOMPILE hints, and encrypted code.' ELSE
2884-
SUBSTRING(
2881+
Warnings = SUBSTRING(
28852882
CASE WHEN warning_no_join_predicate = 1 THEN ', No Join Predicate' ELSE '' END +
28862883
CASE WHEN compile_timeout = 1 THEN ', Compilation Timeout' ELSE '' END +
28872884
CASE WHEN compile_memory_limit_exceeded = 1 THEN ', Compile Memory Limit Exceeded' ELSE '' END +
@@ -2934,7 +2931,6 @@ SELECT DISTINCT
29342931
CASE WHEN low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END +
29352932
CASE WHEN long_running_low_cpu = 1 THEN + 'Long Running With Low CPU' ELSE '' END
29362933
, 2, 200000)
2937-
END
29382934
FROM ##bou_BlitzCacheProcs b
29392935
WHERE SPID = @@SPID
29402936
AND QueryType LIKE 'Statement (parent%'
@@ -2947,9 +2943,32 @@ ON b.SqlHandle = s.SqlHandle
29472943
WHERE QueryType LIKE 'Procedure or Function%'
29482944
OPTION(RECOMPILE);
29492945

2946+
RAISERROR('Checking for plans with >128 levels of nesting', 0, 1) WITH NOWAIT;
2947+
WITH plan_handle AS (
2948+
SELECT b.PlanHandle
2949+
FROM ##bou_BlitzCacheProcs b
2950+
CROSS APPLY sys.dm_exec_text_query_plan(b.PlanHandle, 0, -1) tqp
2951+
CROSS APPLY sys.dm_exec_query_plan(b.PlanHandle) qp
2952+
WHERE tqp.encrypted = 0
2953+
AND b.SPID = @@SPID
2954+
AND (qp.query_plan IS NULL
2955+
AND tqp.query_plan IS NOT NULL)
2956+
)
2957+
UPDATE b
2958+
SET Warnings = ISNULL('Your query plan is >128 levels of nested nodes, and can''t be converted to XML. Use SELECT * FROM sys.dm_exec_text_query_plan('+ CONVERT(VARCHAR(128), ph.PlanHandle, 1) + ', 0, -1) to get more information'
2959+
, 'We couldn''t find a plan for this query. Possible reasons for this include dynamic SQL, RECOMPILE hints, and encrypted code.')
2960+
FROM ##bou_BlitzCacheProcs b
2961+
LEFT JOIN plan_handle ph ON
2962+
b.PlanHandle = ph.PlanHandle
2963+
WHERE b.QueryPlan IS NULL
2964+
AND b.SPID = @@SPID
2965+
OPTION (RECOMPILE);
2966+
2967+
RAISERROR('Checking for plans with no warnings', 0, 1) WITH NOWAIT;
29502968
UPDATE ##bou_BlitzCacheProcs
29512969
SET Warnings = 'No warnings detected.'
29522970
WHERE Warnings = '' OR Warnings IS NULL
2971+
AND SPID = @@SPID
29532972
OPTION (RECOMPILE);
29542973

29552974

0 commit comments

Comments
 (0)