SQL-Engineering-Handbook

AI-Assisted Query Tuning Workflow (Safe Copilot Usage)

Introduction

An LLM can read an EXPLAIN plan, suggest an index, and rewrite a query in seconds — and it can also confidently suggest an index that doesn’t help, invent an EXPLAIN line that doesn’t exist in your engine’s output, or “optimize” a query into one that returns different results. This lesson is not about whether to use AI assistance for query tuning — it’s about the specific, mandatory validation workflow that makes AI assistance safe to use in a codebase this handbook expects to run in production. Every technique here treats the model’s output the way this handbook treats a junior engineer’s first draft: a starting hypothesis to verify, not a conclusion to trust.

Schema note: this lesson’s SQL lab uses the handbook’s shared transactions table (part of 00_Schema.sql), the module’s documented extension to the core employes/departments/locations schema.

Learning Objectives

Why This Exists

The failure mode this lesson addresses isn’t “the AI gave a bad answer” — it’s “the AI gave a plausible-sounding, syntactically valid, wrong answer, and it shipped because nobody validated it the way they’d validate a human’s suggestion.” An LLM has no execution environment by default; it is pattern-completing what a correct-looking answer resembles, not running your query against your data. Every technique in this lesson exists to close that specific gap.

Safe Prompting Patterns for EXPLAIN Analysis

The single highest-leverage habit: paste the actual EXPLAIN output, not a description of the problem. “My query is slow” produces generic advice. A pasted plan with row-count estimates, join types, and index names produces analysis the model can ground in specifics instead of guessing.

Index Suggestion Validation

Never create an AI-suggested index directly. Run it through the same checklist you’d apply to a human’s suggestion, using Lesson 03 as the ground truth:

  1. Does the suggested index actually match the query’s filter and sort columns, in the right order? A model can suggest column order that looks reasonable but doesn’t match the leftmost-prefix rule an engine actually uses.
  2. Does an index that already (partially) covers this already exist? Ask the model directly, but verify yourself against the real schema — a model without full visibility into every existing index will not reliably catch a near-duplicate.
  3. What’s the write-side cost? A model asked only “will this index speed up my query” has no incentive to volunteer the insert/update penalty Lesson 03 treats as a mandatory tradeoff consideration — ask for it explicitly.
  4. Validate with EXPLAIN after creating the index in a non-production environment, exactly as you would for a human-suggested index. The AI suggested it; the query planner decides whether it’s used.

Rewrite Verification Against the Performance Smells Catalog

Cross-check every AI-suggested rewrite against PERFORMANCE_SMELLS.md (and this handbook’s COST_SMELLS.md, Lesson 10) in both directions:

Red Flags for Hallucinated Optimizations

Red flag Why it happens What to do
A cited EXPLAIN field or plan-node name that doesn’t exist in your engine/version Models blend syntax and output formats across Postgres, MySQL, SQL Server, and older engine versions Cross-check every cited field name against your actual EXPLAIN output before trusting the analysis built on it
A suggested index that the model claims will be used, stated with certainty The model cannot run your query planner; it is predicting, not observing Never treat a model’s plan-usage claim as verified — only your own EXPLAIN run against the real schema and data volume confirms it
Specific performance numbers (“this will be 10x faster”) with no measurement behind them Precise-sounding numbers are a known confidence-inflation pattern separate from actual accuracy Treat any unmeasured numeric claim as illustrative at best; benchmark it yourself (Lesson 11) before repeating the number anywhere
A rewrite that changes aggregation, JOIN type, or NULL-handling semantics while claiming “identical results” Semantic-preservation is exactly the kind of subtle correctness property a model can get wrong while sounding confident Always diff the actual result sets — see “Rewrite Verification” above; never accept a semantic-equivalence claim without checking it
Confident engine-specific syntax for a feature that doesn’t exist in your engine or version Training data blends across engines and versions without a reliable boundary Check the suggested syntax against your engine’s actual current documentation before running it anywhere near production

Five Copy-Paste Prompt Templates

1. EXPLAIN plan analysis (grounded, not generic)

I'm running [ENGINE + VERSION, e.g. "PostgreSQL 16"]. Here is my schema:
[PASTE CREATE TABLE + existing indexes]

Here is my query and its EXPLAIN (ANALYZE, BUFFERS) output:
[PASTE QUERY]
[PASTE EXPLAIN OUTPUT]

Walk through the plan node by node. For each node, tell me what it's doing
and whether the estimated vs. actual row counts suggest a cardinality
misestimate. Do not suggest a fix yet -- I want your diagnosis first.

2. Index suggestion with mandatory tradeoff disclosure

Given this schema and query [PASTE], suggest an index that would help.
For your suggestion, explicitly state:
1. The exact column order and why that order matters for this query
2. Whether an existing index already partially covers this
3. The write-side cost (INSERT/UPDATE/DELETE overhead) of adding it
4. One scenario where this index would NOT be chosen by the planner

3. Rewrite with correctness-preservation proof

Here is a query I want to optimize: [PASTE]

Propose a rewrite. Then, separately, write a verification query I can run
that proves the original and rewritten queries return identical result
sets (same rows, same order if order matters, same NULL handling) against
my actual data -- not just an argument for why they should be equivalent.

4. Adversarial self-check (“what would make this wrong”)

You suggested [PASTE THE MODEL'S PRIOR SUGGESTION].

Before I implement this: what assumption about my data distribution,
cardinality, or engine version would make this suggestion NOT help, or
actively make performance worse? Be specific, not generic.

5. Cross-check against a known smell catalog

Here is a catalog of performance anti-patterns my team maintains:
[PASTE RELEVANT ENTRIES FROM PERFORMANCE_SMELLS.md / COST_SMELLS.md]

Here is a query and a proposed rewrite: [PASTE BOTH]

Does the rewrite eliminate any smell in this catalog? Does it introduce
any smell in this catalog that the original didn't have? Answer both
questions explicitly, referencing specific catalog entries.

Mandatory Human Validation Steps

No AI-assisted tuning output reaches production without all of the following, regardless of how confident the model’s response sounded:

  1. EXPLAIN re-run against the real schema, on a non-production environment, by a human — not accepted from the model’s claim
  2. Result-set diff confirming correctness preservation, for any rewrite
  3. Cross-check against PERFORMANCE_SMELLS.md / COST_SMELLS.md in both directions (smell eliminated, no new smell introduced)
  4. A benchmark run through Lesson 11’s CI gate, or an equivalent manual benchmark, before merging — an AI-suggested fix is a hypothesis, and Lesson 11’s entire premise is that hypotheses about performance get tested, not trusted
  5. Sign-off from a human who understands why the change works, not just that the model said it would — this handbook’s interview-insight pattern throughout every module exists precisely so an engineer can explain their own tuning decisions, and that standard doesn’t relax because a model made the initial suggestion

LLM Limitations Specific to Query Tuning

Engineering Notes

MySQL / PostgreSQL / SQL Server Notes

Common Mistakes

Anti-patterns

Edge Cases

Troubleshooting Guidance

Scalability Considerations

AI-assisted tuning suggestions are generated without knowledge of how your data will grow. A suggestion validated as correct and beneficial against today’s data volume still needs the same re-validation-over-time treatment Lesson 11 applies to every other performance-relevant change — an AI-suggested index that helped at 100K rows is not guaranteed to still be the right index at 100M rows, and nothing about the AI-assisted origin of that index makes it exempt from Lesson 11’s CI gate or this handbook’s ongoing tuning workflow.

Additional Dialect Notes (Oracle, SQLite, DuckDB)

Interview Questions

Summary

AI assistance genuinely accelerates the hypothesis-generation stage of query tuning — it does not accelerate, and should never replace, the verification stage this handbook has taught as mandatory from the start. Treat every AI-suggested index, rewrite, or diagnosis as an unverified claim from a fluent, occasionally wrong source, and route it through the same EXPLAIN-and-benchmark discipline you’d apply to a suggestion from any other engineer — confident tone included.

Practice Challenges

  1. Take a query from Lesson 04 (Joins), intentionally ask an AI assistant for a “faster” rewrite without providing the schema, and document every unstated assumption in its answer using this lesson’s “Red Flags” table.
  2. Using Prompt Template 3, get an AI-suggested rewrite for one of Module 18’s cost-focused queries, then write the result-set diff query that proves (or disproves) correctness preservation yourself.

Further Reading