Skip to content

Commit 4ea809c

Browse files
authored
Merge pull request #3869 from erikdarlingdata/3868-blitzcache-oversized-plans
sp_BlitzCache: return oversized plans inline instead of manual query fallback
2 parents d4dc7e8 + 118faa4 commit 4ea809c

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

sp_BlitzCache.sql

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5140,25 +5140,46 @@ WHERE QueryType LIKE 'Procedure or Function%'
51405140
AND SPID = @@SPID
51415141
OPTION (RECOMPILE);
51425142

5143-
RAISERROR('Checking for plans with >128 levels of nesting', 0, 1) WITH NOWAIT;
5144-
WITH plan_handle AS (
5145-
SELECT b.PlanHandle
5146-
FROM ##BlitzCacheProcs b
5147-
CROSS APPLY sys.dm_exec_text_query_plan(b.PlanHandle, 0, -1) tqp
5148-
CROSS APPLY sys.dm_exec_query_plan(b.PlanHandle) qp
5149-
WHERE tqp.encrypted = 0
5150-
AND b.SPID = @@SPID
5151-
AND (qp.query_plan IS NULL
5152-
AND tqp.query_plan IS NOT NULL)
5153-
)
5154-
UPDATE b
5155-
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'
5156-
, 'We couldn''t find a plan for this query. More info on possible reasons: https://www.brentozar.com/go/noplans')
5157-
FROM ##BlitzCacheProcs b
5158-
LEFT JOIN plan_handle ph ON
5159-
b.PlanHandle = ph.PlanHandle
5143+
/* Populate oversized plans as processing instructions so they're clickable in SSMS */
5144+
RAISERROR('Checking for plans with >128 levels of nesting', 0, 1) WITH NOWAIT;
5145+
UPDATE
5146+
b
5147+
SET
5148+
b.QueryPlan =
5149+
(
5150+
SELECT
5151+
[processing-instruction(query_plan)] =
5152+
N'-- ' + NCHAR(13) + NCHAR(10) +
5153+
N'-- This is a huge query plan.' + NCHAR(13) + NCHAR(10) +
5154+
N'-- Remove the headers and footers, save it as a .sqlplan file, and re-open it.' + NCHAR(13) + NCHAR(10) +
5155+
NCHAR(13) + NCHAR(10) +
5156+
REPLACE(tqp.query_plan, N'<RelOp', NCHAR(13) + NCHAR(10) + N'<RelOp') +
5157+
NCHAR(13) + NCHAR(10)
5158+
FOR
5159+
XML
5160+
PATH(N''),
5161+
TYPE
5162+
),
5163+
b.Warnings = 'This is a huge query plan (>128 levels of nesting). Click the plan link, remove the headers and footers, and save it as a .sqlplan file to view it.'
5164+
FROM ##BlitzCacheProcs AS b
5165+
CROSS APPLY sys.dm_exec_text_query_plan(b.PlanHandle, COALESCE(b.StatementStartOffset, 0), COALESCE(b.StatementEndOffset, -1)) AS tqp
5166+
CROSS APPLY sys.dm_exec_query_plan(b.PlanHandle) AS qp
51605167
WHERE b.QueryPlan IS NULL
5161-
AND b.SPID = @@SPID
5168+
AND b.SPID = @@SPID
5169+
AND tqp.encrypted = 0
5170+
AND qp.query_plan IS NULL
5171+
AND tqp.query_plan IS NOT NULL
5172+
OPTION (RECOMPILE);
5173+
5174+
/* Handle truly missing plans (encrypted, evicted, etc.) */
5175+
UPDATE
5176+
b
5177+
SET
5178+
b.Warnings = 'We couldn''t find a plan for this query. More info on possible reasons: https://www.brentozar.com/go/noplans'
5179+
FROM ##BlitzCacheProcs AS b
5180+
WHERE b.QueryPlan IS NULL
5181+
AND (b.Warnings IS NULL OR b.Warnings = '')
5182+
AND b.SPID = @@SPID
51625183
OPTION (RECOMPILE);
51635184

51645185
RAISERROR('Checking for plans with no warnings', 0, 1) WITH NOWAIT;

0 commit comments

Comments
 (0)