Optimize SQL Query — Joins, Cardinality, and Wasted Work
A five-table join that got slow as data grew: establish cardinality first, check each join strategy against the data shape, and hunt the fan-out doing wasted work.
Fast for most customers, 30x slower for the largest two — the parameter sniffing signature. SQL Server tuning with actual plans, clustering-key economics, and CONVERT_IMPLICIT hunting.
SQL Server tuning has engine-specific realities that generic SQL advice misses entirely. This prompt carries them: demand actual execution plans with SET STATISTICS IO, TIME ON (estimated plans hide spills and estimate-vs-actual gaps); reason about the clustered index — every nonclustered index carries the clustering key, so a wide key taxes every index; check parameter sniffing when performance varies by parameter — the loaded scenario is exactly that signature, fast for most customers and 30x slower for the largest; and watch for CONVERT_IMPLICIT in the plan, where a VARCHAR/NVARCHAR mismatch turns seeks into scans. A high-execution Key Lookup gets flagged as the covering-index candidate it is.
Demand the actual plan
SET STATISTICS IO, TIME ON plus the actual plan — estimates hide exactly what matters.
Check the sniffing signature
Performance varying by parameter value points at a plan compiled for unrepresentative values — evidence before OPTIMIZE FOR.
Cost against the clustering key
Every index recommendation accounts for what the clustered index makes it carry.
Estimated plans hide the two things this tuning depends on. The DATABASE CONTEXT requires the Actual Execution Plan with SET STATISTICS IO, TIME ON, since estimates conceal spills and estimate-vs-actual gaps. Where no plan is pasted, the prompt refuses to invent one and instead outputs the exact capture command, marking every plan-dependent conclusion as pending that evidence.
It's SQL Server-specific and will misfire elsewhere. The engine-specific rules — CONVERT_IMPLICIT hunting for VARCHAR/NVARCHAR mismatches, clustering-key economics where every nonclustered index carries the clustering key, and OPTIMIZE FOR/RECOMPILE for parameter sniffing — only hold on SQL Server. notFor points Postgres and MySQL to their own platform modes because the realities differ.
That signature points straight at parameter sniffing. The prompt flags performance that varies by parameter as a plan compiled for unrepresentative values, and the loaded scenario — same query, same plan cache, 30x slower for the two largest customers — is exactly that pattern. It still gates OPTIMIZE FOR or RECOMPILE behind evidence from the actual plan rather than applying them blind.
A five-table join that got slow as data grew: establish cardinality first, check each join strategy against the data shape, and hunt the fan-out doing wasted work.
"Optimize this query" gets generic indexing advice. The optimization contract demands evidence: real bottlenecks, justified indexes with their write tax, and no invented plans.
Optimize what the engine does, not what the SQL looks like: cost concentration, estimate-vs-actual gaps, and plan warnings — with forensic evidence rules.
"Fix this error" gets guesses. The investigation contract gets a ten-stage diagnosis: facts separated from assumptions, alternatives weighed, fixes justified.
"Review this code" gets shallow comments. The review contract gets findings with severities, a checklist, and a verdict.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
Build evidence-based SQL optimization prompts — goal, platform, and the evidence you have turn into a query tuning contract.