Skip to content

Commit 70c0acf

Browse files
committed
#3669 - AI - skip calls for parents with kids
If a parent proc has children in the list, only call it for the statements, not the parent. Added prompt context for AI to focus it on the specific statements. Working on #3669.
1 parent 04ca279 commit 70c0acf

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

sp_BlitzCache.sql

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,7 +5111,7 @@ BEGIN
51115111
RAISERROR('Building AI prompts for query plans', 0, 1) WITH NOWAIT;
51125112

51135113
/* Update ai_prompt column with query metrics for rows that have query plans */
5114-
UPDATE ##BlitzCacheProcs
5114+
UPDATE p
51155115
SET ai_prompt = N'Here are the performance metrics we are seeing in production, as measured by the plan cache:
51165116
51175117
Database: ' + ISNULL(DatabaseName, N'Unknown') + N'
@@ -5172,16 +5172,45 @@ Here are the warnings that popular query analysis tool sp_BlitzCache detected an
51725172
Query Text (which is cut off for long queries):
51735173
' + ISNULL(LEFT(QueryText, 4000), N'N/A') + N'
51745174
5175+
' + CASE WHEN QueryType LIKE N'Statement (parent%' THEN N' 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. ' ELSE N' ' END + N'
5176+
51755177
XML Execution Plan:
51765178
' + ISNULL(CAST(QueryPlan AS NVARCHAR(MAX)), N'N/A') + N'
51775179
51785180
Thank you.'
5179-
WHERE SPID = @@SPID
5180-
AND QueryPlan IS NOT NULL
5181+
FROM ##BlitzCacheProcs p
5182+
WHERE p.SPID = @@SPID
5183+
AND p.QueryPlan IS NOT NULL
5184+
AND NOT (p.QueryType LIKE '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. */
5185+
AND EXISTS
5186+
(
5187+
SELECT 1
5188+
FROM ##BlitzCacheProcs AS S
5189+
WHERE
5190+
S.SPID = p.SPID
5191+
AND S.DatabaseName = p.DatabaseName
5192+
AND S.PlanHandle = p.PlanHandle
5193+
AND S.QueryType LIKE 'Statement (parent %'
5194+
AND
5195+
/* Procedure name from "Procedure or Function: [dbo].[usp_X]" */
5196+
LTRIM(RTRIM(SUBSTRING(
5197+
p.QueryType,
5198+
CHARINDEX(':', p.QueryType) + 1,
5199+
8000
5200+
))) =
5201+
/* Procedure name from "Statement (parent [dbo].[usp_X])" */
5202+
LTRIM(RTRIM(SUBSTRING(
5203+
S.QueryType,
5204+
LEN('Statement (parent ') + 1,
5205+
CHARINDEX(')', S.QueryType, LEN('Statement (parent ') + 1)
5206+
- (LEN('Statement (parent ') + 1)
5207+
)))
5208+
)
5209+
)
51815210
OPTION (RECOMPILE);
51825211

51835212
IF @Debug = 2
5184-
SELECT 'Before Calling AI' AS ai_stage, SqlHandle, QueryHash, PlanHandle, QueryPlan, ai_prompt, ai_advice, ai_raw_response
5213+
SELECT 'After setting up ai_prompt, before calling AI' AS ai_stage, SqlHandle, QueryHash, PlanHandle, QueryPlan, ai_prompt, ai_advice, ai_raw_response
51855214
FROM ##BlitzCacheProcs
51865215
WHERE SPID = @@SPID;
51875216

@@ -5332,7 +5361,6 @@ Thank you.'
53325361
FROM ##BlitzCacheProcs
53335362
WHERE SPID = @@SPID;
53345363

5335-
RAISERROR('AI analysis complete', 0, 1) WITH NOWAIT;
53365364
END;
53375365
ELSE
53385366
BEGIN

0 commit comments

Comments
 (0)