Skip to content

Commit 2caf971

Browse files
authored
Merge pull request BrentOzarULTD#3810 from BrentOzarULTD/3809_sp_BlitzCache_constitution
BrentOzarULTD#3809 sp_BlitzCache constitution.md
2 parents d5cdf1c + 828b087 commit 2caf971

1 file changed

Lines changed: 94 additions & 3 deletions

File tree

sp_BlitzCache.sql

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5118,12 +5118,103 @@ OPTION (RECOMPILE);
51185118
IF @AI >= 1
51195119
BEGIN
51205120
RAISERROR('Building AI prompts for query plans', 0, 1) WITH NOWAIT;
5121-
5121+
5122+
/* If the target database has a database-level extended property named CONSTITUTION.md,
5123+
include it in the prompt as additional guidance for the LLM. */
5124+
IF OBJECT_ID('tempdb..#ai_constitution', 'U') IS NOT NULL
5125+
DROP TABLE #ai_constitution;
5126+
5127+
CREATE TABLE #ai_constitution
5128+
(
5129+
DatabaseName SYSNAME NOT NULL PRIMARY KEY,
5130+
Constitution NVARCHAR(MAX) NULL
5131+
);
5132+
5133+
DECLARE @ai_db SYSNAME,
5134+
@ai_sql NVARCHAR(MAX),
5135+
@ai_constitution NVARCHAR(MAX),
5136+
@ai_engine_edition INT;
5137+
5138+
SET @ai_engine_edition = CONVERT(INT, SERVERPROPERTY('EngineEdition'));
5139+
5140+
DECLARE ai_db_cursor CURSOR LOCAL FAST_FORWARD FOR
5141+
SELECT DISTINCT DatabaseName
5142+
FROM ##BlitzCacheProcs
5143+
WHERE SPID = @@SPID
5144+
AND QueryPlan IS NOT NULL
5145+
AND DatabaseName IS NOT NULL;
5146+
5147+
OPEN ai_db_cursor;
5148+
5149+
FETCH NEXT FROM ai_db_cursor INTO @ai_db;
5150+
5151+
WHILE @@FETCH_STATUS = 0
5152+
BEGIN
5153+
BEGIN TRY
5154+
SET @ai_constitution = NULL;
5155+
5156+
/* Note: database-level extended properties live in sys.extended_properties with class=0, major_id=0, minor_id=0 */
5157+
5158+
/* Azure SQL DB does not allow cross-database three-part names. In that environment, only read from the current database. */
5159+
IF @ai_engine_edition = 5
5160+
BEGIN
5161+
IF @ai_db = DB_NAME()
5162+
SET @ai_sql = N'SELECT @c = CAST(value AS NVARCHAR(MAX))
5163+
FROM sys.extended_properties
5164+
WHERE class = 0
5165+
AND major_id = 0
5166+
AND minor_id = 0
5167+
AND name = N''CONSTITUTION.md'';';
5168+
ELSE
5169+
SET @ai_sql = NULL;
5170+
END
5171+
ELSE
5172+
BEGIN
5173+
SET @ai_sql = N'SELECT @c = CAST(value AS NVARCHAR(MAX))
5174+
FROM ' + QUOTENAME(@ai_db) + N'.sys.extended_properties
5175+
WHERE class = 0
5176+
AND major_id = 0
5177+
AND minor_id = 0
5178+
AND name = N''CONSTITUTION.md'';';
5179+
END;
5180+
5181+
IF @ai_sql IS NOT NULL
5182+
BEGIN
5183+
EXEC sys.sp_executesql
5184+
@ai_sql,
5185+
N'@c NVARCHAR(MAX) OUTPUT',
5186+
@c = @ai_constitution OUTPUT;
5187+
END;
5188+
5189+
IF @ai_constitution IS NOT NULL AND LEN(@ai_constitution) > 0
5190+
INSERT INTO #ai_constitution (DatabaseName, Constitution)
5191+
VALUES (@ai_db, @ai_constitution);
5192+
END TRY
5193+
BEGIN CATCH
5194+
/* If we can't read it (permissions, offline, etc), just skip. */
5195+
END CATCH;
5196+
5197+
FETCH NEXT FROM ai_db_cursor INTO @ai_db;
5198+
END;
5199+
5200+
CLOSE ai_db_cursor;
5201+
DEALLOCATE ai_db_cursor;
5202+
5203+
/* Update ai_prompt column with query metrics for rows that have query plans */
5204+
UPDATE p
5205+
SET ai_prompt = COALESCE(ai_prompt, N'') + N'---' + @nl + N'This database has an extended property named CONSTITUTION.md that provides additional guidance for AI analysis. Here is the content of that property:' + @nl + N'---' + @nl + c.Constitution + @nl + N'---' + @nl
5206+
FROM ##BlitzCacheProcs p
5207+
INNER JOIN #ai_constitution c ON p.DatabaseName = c.DatabaseName
5208+
WHERE p.SPID = @@SPID
5209+
AND c.Constitution IS NOT NULL
5210+
AND LEN(c.Constitution) > 0;
5211+
5212+
51225213
/* Update ai_prompt column with query metrics for rows that have query plans */
51235214
UPDATE p
5124-
SET ai_prompt = N'Here are the performance metrics we are seeing in production, as measured by the plan cache:
5215+
SET ai_prompt = COALESCE(ai_prompt, N'') + N'Here are the performance metrics we are seeing in production, as measured by the plan cache:
51255216
5126-
Database: ' + ISNULL(DatabaseName, N'Unknown') + N'
5217+
Database: ' + ISNULL(p.DatabaseName, N'Unknown') + N'
51275218
Query Type: ' + ISNULL(QueryType, N'Unknown') + N'
51285219
Execution Count: ' + ISNULL(CAST(ExecutionCount AS NVARCHAR(30)), N'N/A') + N'
51295220
Executions Per Minute: ' + ISNULL(CAST(ExecutionsPerMinute AS NVARCHAR(30)), N'N/A') + N'

0 commit comments

Comments
 (0)