You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sp_BlitzCache.sql
+23-11Lines changed: 23 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -5201,7 +5201,7 @@ BEGIN
5201
5201
SELECT DISTINCT DatabaseName
5202
5202
FROM ##BlitzCacheProcs
5203
5203
WHERE SPID =@@SPID
5204
-
AND QueryPlan ISNOTNULL
5204
+
AND(QueryPlan ISNOTNULLOR QueryText ISNOTNULL)
5205
5205
AND DatabaseName ISNOTNULL;
5206
5206
5207
5207
OPEN ai_db_cursor;
@@ -5330,17 +5330,16 @@ Plan Info:
5330
5330
Here are the warnings that popular query analysis tool sp_BlitzCache detected and suggested that we focus on - although there may be more issues, too: '+ISNULL(Warnings, N'None') + N'
5331
5331
5332
5332
Query Text (which is cut off for long queries):
5333
-
'+ISNULL(LEFT(QueryText, 4000), N'N/A')+ N'
5333
+
'+CASEWHEN QueryText ISNULLTHENN'(Query text could not be retrieved.)'ELSELEFT(QueryText, 4000)END+ N'
5334
5334
5335
5335
'+CASEWHEN QueryType LIKEN'Statement (parent%'THENN' The above query is part of a batch, stored procedure, or function, so other queries may show up in the query plan. However, those other queries are irrelevant here. Focus on this specific query above, because it is one of the most resource-intensive queries in the batch. The execution plan below includes other statements in the batch, but ignore those and focus only the query above and its specific plan in the batch below. 'ELSEN' 'END+ N'
5336
5336
5337
5337
XML Execution Plan:
5338
-
'+ISNULL(CAST(QueryPlan ASNVARCHAR(MAX)), N'N/A')+ N'
5338
+
'+CASEWHEN QueryPlan ISNULLTHENN'(Query plan could not be retrieved.)'ELSECAST(QueryPlan ASNVARCHAR(MAX))END+ N'
5339
5339
5340
5340
Thank you.'
5341
5341
FROM ##BlitzCacheProcs p
5342
5342
WHEREp.SPID=@@SPID
5343
-
AND (@AI =2ORp.QueryPlanISNOTNULL) /* For @AI = 1, skip null plans (no value calling AI without a plan); for @AI = 2, build prompts even when plan is null */
5344
5343
ANDNOT (p.QueryTypeLIKE'Procedure or Function:%'/* This and the below exists query makes sure that we don't get advice for parent procs, only their statements, if the statements are in our result set. */
5345
5344
ANDEXISTS
5346
5345
(
@@ -5369,6 +5368,14 @@ Thank you.'
5369
5368
)
5370
5369
OPTION (RECOMPILE);
5371
5370
5371
+
/* If both query text and query plan are null, override with a simple message - no metrics or system prompt needed */
5372
+
UPDATE ##BlitzCacheProcs
5373
+
SET ai_prompt =N'Prompt not generated because we can''t find the query text or query plan.'
5374
+
WHERE SPID =@@SPID
5375
+
AND QueryText ISNULL
5376
+
AND QueryPlan ISNULL
5377
+
OPTION (RECOMPILE);
5378
+
5372
5379
IF @Debug =2
5373
5380
SELECT'After setting up ai_prompt, before calling AI'AS ai_stage, SqlHandle, QueryHash, PlanHandle, QueryPlan, ai_prompt, ai_advice, ai_raw_response
0 commit comments