Prompt Engineering SQL Performance

SQL Optimization Prompt — the Evidence-Based Contract

"Optimize this query" gets generic indexing advice. The optimization contract demands evidence: real bottlenecks, justified indexes with their write tax, and no invented plans.

Overview

Most SQL optimization prompts get answers that would read the same for any query: add an index, avoid SELECT *. The optimization contract structures the analysis instead: bottlenecks identified with their cost share, opportunities ordered by evidenced impact, index recommendations that must name the exact clause they serve and the writes they slow, tradeoffs for every change, and an assumptions ledger — with the non-goals that keep it honest: do not invent execution plans, do not assume indexes exist, do not assume row counts. This setup loads a slow API query whose SQL carries two classic sins — SELECT * and a non-sargable YEAR() filter — for the contract to work on.

Workflow

  1. Bring the evidence you have

    Query, plan, row counts, indexes, measured symptoms — the contract adapts to what is provided and refuses to fake the rest.

  2. Hold the analysis order

    Bottlenecks with cost shares first, opportunities by evidenced impact second — never recommendations before diagnosis.

  3. Check every index's pedigree

    Each recommendation names the predicates it serves, justifies its column order, and states its write tax.

Why This Works

  • Evidence requirements eliminate the advice that fits every query and helps none
  • Impact ordering forces prioritization instead of a flat list of tips
  • The write-tax rule surfaces the cost that index advice always omits

Best for

  • Developers using chat AIs to tune real queries
  • Queries where generic advice has already failed
  • Teams standardizing how SQL performance work is requested

Not for

  • Finding where an application's time goes in the first place — that's the Debugging Prompt Generator
  • Reviewing the code around the query — that's the Code Review Prompt Generator

Use cases

  • Getting query-specific analysis instead of generic database advice
  • Forcing every index recommendation to name its clause and its cost
  • Keeping missing evidence honest: commands to gather it, not guesses

Tip: Save time by exploring related resources and tools that integrate with this workflow.

Explore all resources