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_BlitzIndex.sql
+237-6Lines changed: 237 additions & 6 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,21 @@ 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
+
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. Do not describe the table: you are working with other very senior database developers who understand SQL Server deeply, so get straight to the point with your recommendations and scripts.
1119
+
1120
+
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.
1121
+
1122
+
If indexes are not being used, drop them. If duplicate or near-duplicate indexes exist, merge them together or keep the widest ones. Existing indexes that start with different leading columns should not be considered duplicates.
1114
1123
1115
-
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.
1124
+
Include CREATE INDEX and DROP INDEX scripts. Include undo scripts in comments to back out your work if something goes wrong. Use the /* */ style for comments, not --, to make it easier for the customer to copy and paste your scripts without accidentally missing a line.
1125
+
1126
+
When working with missing index suggestions from SQL Server, keep in mind that they are ordered equality vs inequality search in the query, then by the column order of the table. The column order is nowhere near scientific, and can be rearranged if necessary for performance.
1127
+
1128
+
Focus only on nonclustered rowstore indexes. Do not suggest changes for clustered indexes, columnstore indexes, memory-optimized indexes, XML indexes, JSON indexes, or other specialized index types.
1116
1129
1117
1130
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.';
1131
+
END;
1118
1132
1119
1133
IF @AIModel LIKE'gemini%'AND @AIPayloadTemplate ISNULL
1120
1134
SET @AIPayloadTemplate = N'{
@@ -3581,11 +3595,228 @@ BEGIN
3581
3595
OR (magic_benefit_number /CASEWHENcd.create_days< @DaysUptime THENcd.create_daysELSE @DaysUptime END) >=100000)
3582
3596
ORDER BY magic_benefit_number DESC
3583
3597
OPTION ( RECOMPILE );
3584
-
END;
3585
-
ELSE
3598
+
END;
3599
+
ELSE
3586
3600
SELECT'No missing indexes.'AS finding;
3587
3601
3588
-
SELECT
3602
+
/* @AskAI: Index analysis via AI provider */
3603
+
IF @AI >=1AND @TableName ISNOTNULL
3604
+
BEGIN
3605
+
RAISERROR(N'Building AI prompt for index analysis', 0, 1) WITHNOWAIT;
3606
+
3607
+
/* Constitution lookup */
3608
+
DECLARE @ai_constitution NVARCHAR(MAX) =NULL;
3609
+
BEGINTRY
3610
+
SET @StringToExecute = N'SELECT @c = CAST(value AS NVARCHAR(MAX))
3611
+
FROM '+QUOTENAME(@DatabaseName) + N'.sys.extended_properties
IF @ai_constitution ISNOTNULLANDLEN(@ai_constitution) >0
3627
+
SET @CurrentAIPrompt =N'---'+CHAR(13) +CHAR(10)
3628
+
+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)
3707
+
FROM #ForeignKeys
3708
+
ORDER BY foreign_key_name
3709
+
FOR XMLPATH(''), TYPE
3710
+
).value('.', 'NVARCHAR(MAX)'), N'No foreign keys on this table.'+CHAR(13) +CHAR(10));
3711
+
3712
+
/* Closing instruction */
3713
+
SET @CurrentAIPrompt = @CurrentAIPrompt +CHAR(13) +CHAR(10)
3714
+
+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.';
3715
+
3716
+
/* @AI = 1: Call the AI provider */
3717
+
IF @AI =1
3718
+
BEGIN
3719
+
BEGINTRY
3720
+
SET @AIResponseJSON =NULL;
3721
+
3722
+
/* Build payload using the template */
3723
+
SET @AIPayload =REPLACE(@AIPayloadTemplate, N'@AIModel', @AIModel);
0 commit comments