Skip to content

Commit bbb06dd

Browse files
BrentOzarclaude
andcommitted
#3843 AI table column renames for consistency
- Rename PromptNickname to Prompt_Nickname and DefaultPrompt to Default_Prompt in AI Prompts table - Add Model_Nickname column to AI Providers table and rename DefaultModel to Default_Model - Update @aimodel lookup to match against both Model_Nickname and AI_Model - Trim AI payload to Context size when specified (0 or NULL = unlimited) - Update Using_AI.md documentation to reflect all changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent aebef37 commit bbb06dd

3 files changed

Lines changed: 56 additions & 45 deletions

File tree

Documentation/Using_AI.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,32 @@ If you want to override the default prompts, create a table to store your prompt
4545
```sql
4646
CREATE TABLE dbo.Blitz_AI_Prompts
4747
(Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
48-
PromptNickname NVARCHAR(200),
48+
Prompt_Nickname NVARCHAR(200),
4949
AI_System_Prompt NVARCHAR(4000),
5050
Default_Prompt BIT DEFAULT 0);
51-
52-
INSERT INTO dbo.Blitz_AI_Prompts (PromptNickname, Default_Prompt, AI_System_Prompt)
51+
52+
INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt)
5353
VALUES ('sp_BlitzCache Default', 1, '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.
5454
5555
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.
5656
5757
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.');
5858

59-
INSERT INTO dbo.Blitz_AI_Prompts (PromptNickname, Default_Prompt, AI_System_Prompt)
59+
INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt)
6060
VALUES ('sp_BlitzCache Index Tuning', 0, '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.
6161
6262
You have a query that isn''t performing to end user expectations. You have been tasked with making serious improvements to it, quickly, but you are only allowed to make index changes. You are not allowed to make changes to the query, server-level settings, database settings, etc.
6363
6464
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.');
6565

66-
INSERT INTO dbo.Blitz_AI_Prompts (PromptNickname, Default_Prompt, AI_System_Prompt)
66+
INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt)
6767
VALUES ('sp_BlitzCache Deadlock Tuning', 0, '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.
6868
6969
You have a query that is experiencing deadlocks and blocking. You have been tasked with making serious improvements to it, quickly. You are not allowed to change server-level or database-level settings nor make frivolous suggestions like updating statistics. Instead, you need to focus on query changes or index changes that will reduce blocking and deadlocks.
7070
7171
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.');
7272

73-
INSERT INTO dbo.Blitz_AI_Prompts (PromptNickname, Default_Prompt, AI_System_Prompt)
73+
INSERT INTO dbo.Blitz_AI_Prompts (Prompt_Nickname, Default_Prompt, AI_System_Prompt)
7474
VALUES ('sp_BlitzCache Modernize', 0, '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.
7575
7676
You have been given a legacy query that needs to be modernized. Our goals are to make the query run faster, make it easier to understand, easier to maintain, and to take advantage of new features up to and including SQL Server 2025. You have been tasked with making serious improvements to it, quickly, without touching server-level settings, database-level settings, indexes, or statistics.
@@ -195,22 +195,22 @@ CREATE TABLE dbo.Blitz_AI_Providers
195195
Default_Model BIT DEFAULT 0);
196196

197197
/* OpenAI - fast, cheap model, default: */
198-
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, DefaultModel)
198+
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model)
199199
VALUES (N'ChatGPT Fast', N'gpt-5-nano', N'https://api.openai.com/v1/chat/completions',
200200
N'https://api.openai.com/', 30, 1);
201201

202202
/* OpenAI - highest quality, slowest, most expensive model: */
203-
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, DefaultModel)
203+
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model)
204204
VALUES (N'ChatGPT Slow', N'gpt-5.4', N'https://api.openai.com/v1/chat/completions',
205205
N'https://api.openai.com/', 230, 0);
206206

207207
/* Gemini - fast, cheap model: */
208-
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, DefaultModel)
208+
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model)
209209
VALUES (N'Gemini Fast', N'gemini-3-flash-preview', N'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent',
210210
N'https://generativelanguage.googleapis.com/', 30, 0);
211211

212212
/* Gemini - highest quality, slowest, most expensive model: */
213-
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, DefaultModel)
213+
INSERT INTO dbo.Blitz_AI_Providers (Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, Timeout_Seconds, Default_Model)
214214
VALUES (N'Gemini Slow', N'gemini-3-1-pro-preview', N'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent',
215215
N'https://generativelanguage.googleapis.com/', 230, 0);
216216
```
@@ -263,7 +263,7 @@ EXEC sp_BlitzCache @Top = 1, @AI = 1,
263263
| Parameter | Default | Description |
264264
|-----------|---------|-------------|
265265
| `@AI` | 0 | 0 = off, 1 = call AI API, 2 = generate prompt only |
266-
| `@AIModel` | `gpt-5-nano` | Model name. If it starts with `gemini`, the Gemini URL and payload template are used automatically. |
266+
| `@AIModel` | `gpt-5-nano` | Model name or nickname. Matches against both `AI_Model` and `Model_Nickname` in the providers table. If it starts with `gemini`, the Gemini URL and payload template are used automatically. |
267267
| `@AIURL` | `https://api.openai.com/v1/chat/completions` | API endpoint URL. Auto-detected for Gemini models. |
268268
| `@AICredential` | Auto-detected from URL | Database-scoped credential name. Defaults to the root of your `@AIURL` with trailing slash. |
269269
| `@AIConfigTable` | NULL | Three-part name of your providers config table (e.g., `master.dbo.Blitz_AI_Providers`). |
@@ -314,7 +314,7 @@ EXEC sp_BlitzIndex
314314
| Parameter | Default | Description |
315315
|-----------|---------|-------------|
316316
| `@AI` | 0 | 0 = off, 1 = call AI API, 2 = generate prompt only |
317-
| `@AIModel` | `gpt-5-nano` | Model name. Gemini models auto-detect URL and payload template. |
317+
| `@AIModel` | `gpt-5-nano` | Model name or nickname. Matches against both `AI_Model` and `Model_Nickname` in the providers table. Gemini models auto-detect URL and payload template. |
318318
| `@AIURL` | `https://api.openai.com/v1/chat/completions` | API endpoint URL. |
319319
| `@AICredential` | Auto-detected from URL | Database-scoped credential name. |
320320
| `@AIConfigTable` | NULL | Three-part name of your providers config table. |
@@ -342,4 +342,6 @@ With `@AI = 2`:
342342
- **Database context matters** for `@AI = 1`: you must run the query in the database where your credentials are stored, or the API call will fail.
343343
- **Timeout**: The default timeout is 230 seconds. Larger models may need the full timeout; smaller models like `gpt-5-nano` respond in seconds.
344344
- **Cost**: Each call sends your query/index data to the AI provider and costs API credits. Use `@Top = 1` with sp_BlitzCache to limit costs during testing.
345+
- **Context size**: If the `Context` column in your AI Providers table has a value greater than 0, the payload will be trimmed to that length to avoid exceeding the model's context window. Set `Context` to 0 or NULL for unlimited payload size.
346+
- **Model nicknames**: The `@AIModel` parameter matches against both the `AI_Model` and `Model_Nickname` columns in your providers table. This lets you use friendly names like `'ChatGPT Slow'` instead of model identifiers like `'gpt-5.4'`.
345347
- **Security**: Your query text, index definitions, and table structures are sent to the AI provider's API. Do not use this feature if your data or schema is subject to restrictions on external sharing.

sp_BlitzCache.sql

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -874,20 +874,21 @@ END;
874874

875875
CREATE TABLE #ai_providers
876876
(Id INT PRIMARY KEY CLUSTERED,
877+
Model_Nickname NVARCHAR(200),
877878
AI_Model NVARCHAR(100) INDEX AI_Model,
878879
AI_URL NVARCHAR(500),
879880
AI_Database_Scoped_Credential_Name NVARCHAR(500),
880881
AI_Parameters NVARCHAR(4000),
881882
Payload_Template NVARCHAR(4000),
882883
Timeout_Seconds TINYINT,
883884
Context INT,
884-
DefaultModel BIT DEFAULT 0);
885+
Default_Model BIT DEFAULT 0);
885886

886887
CREATE TABLE #ai_prompts
887888
(Id INT PRIMARY KEY CLUSTERED,
888-
PromptNickname NVARCHAR(200) INDEX IX_PromptNickname,
889+
Prompt_Nickname NVARCHAR(200) INDEX IX_Prompt_Nickname,
889890
AI_System_Prompt NVARCHAR(4000),
890-
DefaultPrompt BIT DEFAULT 0);
891+
Default_Prompt BIT DEFAULT 0);
891892

892893
DECLARE
893894
@AIConfigDatabaseName NVARCHAR(128) = CASE WHEN @AIConfigTable IS NULL THEN NULL ELSE PARSENAME(@AIConfigTable, 3) END,
@@ -913,20 +914,20 @@ END;
913914
IF @AIConfigTable IS NOT NULL
914915
BEGIN
915916
RAISERROR(N'Reading values from AI Provider Configuration Table', 0, 1) WITH NOWAIT;
916-
SET @config_sql = N'INSERT INTO #ai_providers (Id, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_Parameters, Payload_Template, Timeout_Seconds, Context, DefaultModel)
917-
SELECT Id, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_Parameters, Payload_Template, Timeout_Seconds, Context, DefaultModel FROM '
917+
SET @config_sql = N'INSERT INTO #ai_providers (Id, Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_Parameters, Payload_Template, Timeout_Seconds, Context, Default_Model)
918+
SELECT Id, Model_Nickname, AI_Model, AI_URL, AI_Database_Scoped_Credential_Name, AI_Parameters, Payload_Template, Timeout_Seconds, Context, Default_Model FROM '
918919
+ CASE WHEN @AIConfigDatabaseName IS NOT NULL THEN (QUOTENAME(@AIConfigDatabaseName) + N'.') ELSE N'' END
919920
+ CASE WHEN @AIConfigSchemaName IS NOT NULL THEN (QUOTENAME(@AIConfigSchemaName) + N'.') ELSE N'' END
920-
+ QUOTENAME(@AIConfigTableName) + N' WHERE DefaultModel = 1 OR @AIModel = AI_Model ; ';
921+
+ QUOTENAME(@AIConfigTableName) + N' WHERE Default_Model = 1 OR @AIModel = AI_Model OR @AIModel = Model_Nickname ; ';
921922
EXEC sp_executesql @config_sql, N'@AIModel NVARCHAR(100)', @AIModel;
922923
END;
923924

924925
IF @AIModel IS NOT NULL AND @AIConfigTable IS NOT NULL
925-
AND NOT EXISTS (SELECT 1 FROM #ai_providers WHERE AI_Model = @AIModel)
926+
AND NOT EXISTS (SELECT 1 FROM #ai_providers WHERE AI_Model = @AIModel OR Model_Nickname = @AIModel)
926927
BEGIN
927928
DECLARE @AIModelRequested NVARCHAR(200) = @AIModel;
928929
DECLARE @AIFallbackModel NVARCHAR(200);
929-
SELECT TOP 1 @AIFallbackModel = AI_Model FROM #ai_providers WHERE DefaultModel = 1 ORDER BY Id;
930+
SELECT TOP 1 @AIFallbackModel = AI_Model FROM #ai_providers WHERE Default_Model = 1 ORDER BY Id;
930931
IF @AIFallbackModel IS NULL SET @AIFallbackModel = N'gpt-5-nano';
931932
RAISERROR('@AIModel "%s" was not found in configuration table %s. Using "%s" instead.',
932933
10, 1, @AIModelRequested, @AIConfigTable, @AIFallbackModel) WITH NOWAIT;
@@ -936,11 +937,11 @@ END;
936937
IF @AIPromptConfigTable IS NOT NULL
937938
BEGIN
938939
RAISERROR(N'Reading values from AI Prompts Table', 0, 1) WITH NOWAIT;
939-
SET @config_sql = N'INSERT INTO #ai_prompts (Id, PromptNickname, AI_System_Prompt, DefaultPrompt)
940-
SELECT Id, PromptNickname, AI_System_Prompt, DefaultPrompt FROM '
940+
SET @config_sql = N'INSERT INTO #ai_prompts (Id, Prompt_Nickname, AI_System_Prompt, Default_Prompt)
941+
SELECT Id, Prompt_Nickname, AI_System_Prompt, Default_Prompt FROM '
941942
+ CASE WHEN @AIPromptDatabaseName IS NOT NULL THEN (QUOTENAME(@AIPromptDatabaseName) + N'.') ELSE N'' END
942943
+ CASE WHEN @AIPromptSchemaName IS NOT NULL THEN (QUOTENAME(@AIPromptSchemaName) + N'.') ELSE N'' END
943-
+ QUOTENAME(@AIPromptTableName) + N' WHERE (@AIPrompt IS NULL AND DefaultPrompt = 1) OR @AIPrompt = PromptNickname ; ';
944+
+ QUOTENAME(@AIPromptTableName) + N' WHERE (@AIPrompt IS NULL AND Default_Prompt = 1) OR @AIPrompt = Prompt_Nickname ; ';
944945
EXEC sp_executesql @config_sql, N'@AIPrompt NVARCHAR(200)', @AIPrompt;
945946
END;
946947

@@ -975,7 +976,7 @@ IF @AI > 0
975976
@AITimeoutSeconds = COALESCE(Timeout_Seconds, 230),
976977
@AIContext = Context
977978
FROM #ai_providers
978-
WHERE DefaultModel = 1
979+
WHERE Default_Model = 1
979980
ORDER BY Id;
980981
ELSE
981982
SELECT TOP 1 @AIModel = AI_Model,
@@ -986,19 +987,19 @@ IF @AI > 0
986987
@AITimeoutSeconds = COALESCE(Timeout_Seconds, 230),
987988
@AIContext = Context
988989
FROM #ai_providers
989-
WHERE AI_Model = @AIModel
990+
WHERE AI_Model = @AIModel OR Model_Nickname = @AIModel
990991
ORDER BY Id;
991992

992993
/* Check the prompts table */
993994
IF @AIPrompt IS NULL
994995
SELECT TOP 1 @AISystemPrompt = AI_System_Prompt
995996
FROM #ai_prompts
996-
WHERE DefaultPrompt = 1
997+
WHERE Default_Prompt = 1
997998
ORDER BY Id;
998999
ELSE
9991000
SELECT TOP 1 @AISystemPrompt = AI_System_Prompt
10001001
FROM #ai_prompts
1001-
WHERE PromptNickname = @AIPrompt
1002+
WHERE Prompt_Nickname = @AIPrompt
10021003
ORDER BY Id;
10031004

10041005
IF @AIModel IS NULL
@@ -1056,7 +1057,7 @@ IF @AI > 0
10561057
@AISystemPrompt AS AISystemPrompt, @AIPayloadTemplate AS AIPayloadTemplate;
10571058
END;
10581059

1059-
IF @AIPrompt IS NOT NULL AND NOT EXISTS (SELECT 1 FROM #ai_prompts WHERE PromptNickname = @AIPrompt)
1060+
IF @AIPrompt IS NOT NULL AND NOT EXISTS (SELECT 1 FROM #ai_prompts WHERE Prompt_Nickname = @AIPrompt)
10601061
BEGIN
10611062
RAISERROR('@AIPrompt was specified but no matching prompt was found in the prompts table.',12,1);
10621063
RETURN;
@@ -5407,7 +5408,10 @@ Thank you.'
54075408
SET @AIPayload = REPLACE(@AIPayloadTemplate, N'@AIModel', @AIModel);
54085409
SET @AIPayload = REPLACE(@AIPayload, N'@AISystemPrompt', REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@AISystemPrompt, '\', '\\'), '"', '\"'), CHAR(13), '\r'), CHAR(10), '\n'), CHAR(9), '\t'));
54095410
SET @AIPayload = REPLACE(@AIPayload, N'@CurrentAIPrompt', REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@CurrentAIPrompt, '\', '\\'), '"', '\"'), CHAR(13), '\r'), CHAR(10), '\n'), CHAR(9), '\t'));
5410-
--SET @AIPayload = REPLACE(@AIPayload, N'@CurrentAIPrompt', @CurrentAIPrompt);
5411+
5412+
/* Trim payload to context size if specified */
5413+
IF @AIContext IS NOT NULL AND @AIContext > 0 AND LEN(@AIPayload) > @AIContext
5414+
SET @AIPayload = LEFT(@AIPayload, @AIContext);
54115415

54125416
IF @Debug = 2
54135417
BEGIN

0 commit comments

Comments
 (0)