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
#3670 sp_BlitzIndex: implement @ai prompt assembly and API call
Add the core AI feature for sp_BlitzIndex single-table mode:
- Declare @AIPayload, @AIResponseJSON, @AIReturnValue, @CurrentAIPrompt
- Make default system prompt index-focused when @TableName is specified
- Build AI prompt with 4 data sections using FOR XML PATH concatenation:
1. Existing indexes (from #IndexSanity + #IndexSanitySize)
2. Missing index suggestions (from #MissingIndexes)
3. Column data types (from #IndexColumns)
4. Foreign keys (from #ForeignKeys)
- Constitution lookup via database extended property
- API call pattern matching sp_BlitzCache (OpenAI + Gemini parsing)
- Result sets: AI Prompt (always), AI Advice/Payload/Raw Response (@ai=1)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: sp_BlitzIndex.sql
+229-2Lines changed: 229 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -155,7 +155,11 @@ DECLARE
155
155
@AIPayloadTemplate NVARCHAR(MAX),
156
156
@AITimeoutSeconds TINYINT,
157
157
@AIAdviceText NVARCHAR(MAX),
158
-
@AIContext INT;
158
+
@AIContext INT,
159
+
@AIPayload NVARCHAR(MAX),
160
+
@AIResponseJSON NVARCHAR(MAX),
161
+
@AIReturnValue INT,
162
+
@CurrentAIPrompt NVARCHAR(MAX);
159
163
160
164
/* If user was lazy and just used @ObjectName with a fully qualified table name, then lets parse out the various parts */
161
165
SET @DatabaseName =COALESCE(@DatabaseName, PARSENAME(@ObjectName, 3)) /* 3 = Database name */
@@ -1110,11 +1114,20 @@ IF @AI > 0
1110
1114
SET @AITimeoutSeconds =230;
1111
1115
1112
1116
IF @AISystemPrompt ISNULLOR @AISystemPrompt =N''
1113
-
SET @AISystemPrompt =N'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment.
1117
+
BEGIN
1118
+
IF @TableName ISNOTNULL
1119
+
SET @AISystemPrompt =N'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment.
1120
+
1121
+
You have been given the existing indexes, missing index suggestions from SQL Server, column data types, and foreign keys for a table. Your job is to recommend index changes: which indexes to add, which to remove as redundant or harmful, and which to modify. Focus on practical changes that will improve the most common query patterns shown by the usage statistics. Include CREATE INDEX and DROP INDEX scripts where appropriate.
1122
+
1123
+
Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.';
1124
+
ELSE
1125
+
SET @AISystemPrompt =N'You are a very senior database developer working with Microsoft SQL Server and Azure SQL DB. You focus on real-world, actionable advice that will make a big difference, quickly. You value everyone''s time, and while you are friendly and courteous, you do not waste time with pleasantries or emoji because you work in a fast-paced corporate environment.
1114
1126
1115
1127
You have a query that isn''t performing to end user expectations. You have been tasked with making serious improvements to it, quickly. You are not allowed to change server-level settings or make frivolous suggestions like updating statistics. Instead, you need to focus on query changes or index changes.
1116
1128
1117
1129
Do not offer followup options: the customer can only contact you once, so include all necessary information, tasks, and scripts in your initial reply. Render your output in Markdown, as it will be shown in plain text to the customer.';
1130
+
END;
1118
1131
1119
1132
IF @AIModel LIKE'gemini%'AND @AIPayloadTemplate ISNULL
1120
1133
SET @AIPayloadTemplate = N'{
@@ -3845,6 +3858,220 @@ BEGIN
3845
3858
IF @ShowColumnstoreOnly =1
3846
3859
RETURN;
3847
3860
3861
+
/* @AskAI: Index analysis via AI provider */
3862
+
IF @AI >=1AND @TableName ISNOTNULL
3863
+
BEGIN
3864
+
RAISERROR(N'Building AI prompt for index analysis', 0, 1) WITHNOWAIT;
3865
+
3866
+
/* Constitution lookup */
3867
+
DECLARE @ai_constitution NVARCHAR(MAX) =NULL;
3868
+
BEGINTRY
3869
+
SET @StringToExecute = N'SELECT @c = CAST(value AS NVARCHAR(MAX))
3870
+
FROM '+QUOTENAME(@DatabaseName) + N'.sys.extended_properties
IF @ai_constitution ISNOTNULLANDLEN(@ai_constitution) >0
3886
+
SET @CurrentAIPrompt =N'---'+CHAR(13) +CHAR(10)
3887
+
+N'This database has an extended property named CONSTITUTION.md that provides additional guidance for AI analysis. Here is the content of that property:'+CHAR(13) +CHAR(10)
+N', Not Trusted: '+CASEWHEN is_not_trusted =1THENN'Yes'ELSEN'No'END+CHAR(13) +CHAR(10) +CHAR(13) +CHAR(10)
3966
+
FROM #ForeignKeys
3967
+
ORDER BY foreign_key_name
3968
+
FOR XMLPATH(''), TYPE
3969
+
).value('.', 'NVARCHAR(MAX)'), N'No foreign keys on this table.'+CHAR(13) +CHAR(10));
3970
+
3971
+
/* Closing instruction */
3972
+
SET @CurrentAIPrompt = @CurrentAIPrompt +CHAR(13) +CHAR(10)
3973
+
+N'Based on the above data, please provide index recommendations for this table. Consider which indexes are redundant, which missing indexes should be created, and whether the current indexing strategy is appropriate for the workload pattern shown by the usage statistics.';
3974
+
3975
+
/* @AI = 1: Call the AI provider */
3976
+
IF @AI =1
3977
+
BEGIN
3978
+
BEGINTRY
3979
+
SET @AIResponseJSON =NULL;
3980
+
3981
+
/* Build payload using the template */
3982
+
SET @AIPayload =REPLACE(@AIPayloadTemplate, N'@AIModel', @AIModel);
0 commit comments