Fixing Slow Reports
Have AI Propose the Index, Then Prove It
Give It Evidence, Not a Complaint
SQL Server 2022. This report is slow. Attached: the query,
the actual execution plan XML, STATISTICS IO output, and
CREATE scripts for the tables with every existing index.
1. Say what the biggest cost is and quote the evidence.
2. Can the query be rewritten to use what already exists?
Do that before proposing any index.
3. Propose at most two indexes. For each: key order and why,
included columns and why, size, and which existing index
it overlaps with.
4. Tell me how I'd prove you wrong.
For the report in the last lesson, a good answer is: fix the filter, then add this.
CREATE INDEX IX_Orders_Status_OrderDate
ON dbo.Orders (Status, OrderDate)
INCLUDE (CustomerId);
Status goes first because it's an exact match. OrderDate goes second because it's a range. CustomerId rides along so there's no key lookup.
Test Each Change by Itself
| Change | Pages read from Orders |
|---|---|
| Nothing | 8,442 |
| Fix the date filter only | 8,442 |
| Add the index only, old filter | 1,735 |
| Both | 41 |
Neither one gets there alone. The rewrite had no index to use, and the index couldn't be used well with the old filter. If you'd tried the rewrite by itself, you'd have called it useless.
With both in place, SQL Server's own suggestion, the green "Missing Index" text in the plan, still wanted this:
CREATE INDEX IX_OrderLines_OrderId
ON dbo.OrderLines (OrderId)
INCLUDE (Quantity, UnitPrice);
It claimed a 94% improvement. Built and measured in a test database, OrderLines went from 6,328 pages to 5,030, and the report from 71 ms to 68 ms. It's still reading the whole thing, just a thinner copy. And that copy takes 5,030 pages of disk that every new order line has to keep up.
So is it a bad index? No. It's a weak fix for this report. But for pulling up a single order's lines it's the difference between 6,328 pages and 3, and the app does that all day. Judge an index against everything that runs, not the one query that brought you here. Neither the plan nor the AI knows your whole workload. You do.
What Indexes Cost
In a test database, 1,000 rows were added to two copies of the same 200,000-row table:
| Indexes besides the primary key | Pages touched to insert 1,000 rows |
|---|---|
| 0 | 298 |
| 5 | 28,708 |
Three of those five overlapped: (A), (A, B) and (A, B, C). The last one can do nearly everything the first two do. That's what you get when you accept every suggestion. Before you add one, list what's there, and see what nobody reads:
SELECT i.name,
s.user_seeks, s.user_scans, s.user_lookups,
s.user_updates
FROM sys.indexes AS i
LEFT JOIN sys.dm_db_index_usage_stats AS s
ON s.object_id = i.object_id
AND s.index_id = i.index_id
AND s.database_id = DB_ID()
WHERE i.object_id = OBJECT_ID('dbo.Orders')
ORDER BY i.index_id;
Lots of updates and no seeks, scans or lookups means you may be paying for something nobody uses. The counts reset when the server restarts, so don't drop anything until you've seen a full cycle, including month end.
The Whole Loop
- Measure. Logical reads and the actual plan.
- Ask AI for a cause, with the evidence attached.
- Change one thing on a copy. Measure again.
- Prove the answer didn't change.
EXCEPTboth ways, plus row counts, from Module 2. - Check the cost. Overlap, size, what it does to writes.
- Ship it like any schema change, from Module 4.
AI can do most of the typing in every one of those steps. Deciding what "right" means, and refusing to believe it until you've seen it, is your part.
Keep the before and after numbers with the change, in the commit message or the ticket. "8,442 pages to 41" ends arguments.
AI proposes an index and the plan says it'll help 94%. What do you do?