Wednesday, February 18, 2026

sp_BlitzCache Can Talk to ChatGPT Now. Here's What It Said.

In a previous post, I got sp_BlitzCache to call ChatGPT. It was super basic. I just performed a call to AI from SQL Server to see how it's done. Worked well. No surprises. Now I've stepped it up a bit and gave it something real to do with index tuning and a code review. Worked well again, but there was a surprise — ChatGPT and SQL Server did not agree.

The Idea

Brent's sp_BlitzCache ChatGPT walkthrough includes a dbo.Blitz_AI config table with an AI_System_Prompt_Override column that lets us change sp_BlitzCache personalities on the fly. Think of the personalities as different AI Models. By changing the personality, we tell sp_BlitzCache what to do:

Personality What It Does
Index tuning only Indexes are the only focus — no query rewrites
Query tuning only Rewrites only — no index changes
Both Index and query changes allowed
Code review smelly T-SQL — not performance related
Blocking / deadlocking Looking for contention problems
Refactoring Rewrite multi-step/rbr query for more efficiency

My Test

I built a 1M-row dbo.Orders table with a clustered PK, a 400-row dbo.Regions lookup, and a usp_OrderSummaryByRegion stored procedure that joins the two tables with a filtered aggregation. Deliberately undertooled — no nonclustered indexes anywhere.

Brent's post covers the setup details, so I won't repeat that here. These are the calls I used to test index tuning and code review against the same stored procedure — with very interesting results:

-- Call 1: Index tuning only - no changes, just advice
EXEC sp_BlitzCache @StoredProcName = 'usp_OrderSummaryByRegion',
    @AI = 1, @AIConfig = 'DBA.dbo.Blitz_AI', @AIModel = 'index tuning';

-- Call 2: Code review only
EXEC sp_BlitzCache @StoredProcName = 'usp_OrderSummaryByRegion',
    @AI = 1, @AIConfig = 'DBA.dbo.Blitz_AI', @AIModel = 'code review';

Round 1: Index Tuning

Ran for 47 seconds, output 2 rows; one for the CREATE PROCEDURE (no advice) and one for the SELECT inside the procedure - with Missing Indexes and AI Advice:

What SQL Server said

The Missing Indexes column held the Optimizer's missing index suggestion with estimated improvement:

Keyed on OrderDate, to include OrderTotal and RegionID. 75% estimated improvement. That is what the optimizer always does — it sees a predicate, builds a key around it, and throws the rest into INCLUDE. Not wrong. But not thinking very hard, either.

What ChatGPT said

This is the AI Advice column output from the query results:

Same table, same stored procedure, but AI's suggested index is different from the Optimizer's.

ChatGPT recommended RegionID, OrderDate as the key; RegionID first, followed by OrderDate, with OrderTotal in the INCLUDE. Then it explained why: the plan shows Regions filtered to ~66 rows by Country, so with RegionID leading, the engine can seek per RegionID for the date range. It estimated a 70-95% reduction in logical reads and called out that CPU, duration, and memory grant would all drop substantially. That is really good.

It also acknowledged SQL Server's suggestion as a valid alternative, explained the trade-off (date-first is better when the date range is very selective and the region set is large), and gave both CREATE INDEX scripts so you could choose:

-- AI's preferred recommendation
CREATE NONCLUSTERED INDEX IX_Orders_RegionID_OrderDate_Includes
    ON dbo.Orders (RegionID ASC, OrderDate ASC)
    INCLUDE (OrderTotal);
GO

-- Alternative (matches the optimizer's missing index hint)
CREATE NONCLUSTERED INDEX IX_Orders_OrderDate_Includes
    ON dbo.Orders (OrderDate ASC)
    INCLUDE (RegionID, OrderTotal);
GO

No query rewrites or passive suggestions. It stayed in its lane. I told it to index tune and it did exactly that very effectively. I am impressed. Even with Chat's index suggestion being different from the Optimizer's — it provided reasoning behind both!

Round 2: Code Review

Same proc, different personality. First try, it ran for 37 seconds and completed with this in AI Advice column:

Error calling AI service: The external rest endpoint execution exceeded the timeout value.

I edited the Timeout_Seconds in Blitz_AI from 30 to 230 seconds and ran again. It ran for 00:01:07 and completed with Missing Indexes and AI Advice. The Missing Indexes output was still the Optimizer's standard 'missing index' suggestion. The AI Advice column was pure code review. Too big to screenshot the entire output here, so I've posted the full AI Advice output on a separate page. Look at the detail it provided! I love how it opens with compliments on my 'clear structure' and readability, and then slams me with 5 issues to address and gives me a proposed revision.

Three things it flagged:

1. EndDate boundary semantics

The WHERE clause uses o.OrderDate >= @StartDate AND o.OrderDate < @EndDate. ChatGPT asked if that was intentional and reminded me that as-is, the EndDate was completely excluded. If I passed '2025-06-30' expecting results to include June 30, they would not be there. It suggested an alternative and told me to document the intention to 'avoid further confusion'. This is the kind of bug that lives in production for years unchecked, because the results look 'close enough'.

2. Parameter data type alignment

The proc declares @Country VARCHAR(50), and ChatGPT told me that if Regions.Country is a different length or type (ie., CHAR(2)), there is risk for implicit conversion. Same thing with @StartDate and @EndDate. I've checked the procedure and the tables -- all data types align, so I'm not sure what prompted this. Maybe just a safety warning? Unsure, but it is a fair flag. Implicit conversions can be costly and I wouldn't have wanted to miss this.

3. Missing input validation

No NULL checks, no range validation, no TRY/CATCH. In ChatGPT's words, "The procedure immediately executes the query without validating inputs". Chat's suggested revision includes the necessary checks with RAISERROR for bad inputs. All I can say is good catch, and I'm surprised that I missed that. Really.

The Point

Same proc, two different personalities. ChatGPT told me to index (RegionID, OrderDate) INCLUDE (OrderTotal) and explained why SQL Server's own suggestion had the key order backwards. Chat's code review also warned that my parameter types might not match my columns and my date boundary might be excluding a day.

That is the power of a constrained prompt. You are not asking 'what do you think?' — you are saying 'you are an index specialist' or 'you are a code reviewer' and letting a focused role produce focused output. Brent was right: the quality of the advice is directly proportional to the work you put into the prompt.

This is going into my toolkit.

Try It

If you have sp_BlitzCache and an OpenAI key, this takes about 20 minutes to set up. Start with Brent's walkthrough for the config table and credentials, pick one of your own problem procs, and test it with the different personalities Brent has coded it for. I will be testing 'Reduce blocking and deadlocking' next.

More to Read:

sqlfingers: sp_BlitzCache Can Talk to ChatGPT Now. Here's How.
Brent Ozar: Get ChatGPT's Advice On Your Queries with sp_BlitzCache
Brent Ozar: Building AI Calls into the First Responder Kit

No comments:

Post a Comment