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.

How to use this resource

  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

FAQ

Will the AI invent an execution plan if I don't give it one?

The NON-GOALS forbid it: do not invent execution plans, do not assume indexes exist, do not assume row counts. When the plan is "not provided", the contract instead states the exact command to capture your engine's actual-execution-plan facility and marks every plan-dependent conclusion as pending that evidence. The sql-optimization-prompt generates this; you run it in your assistant and gather the missing evidence yourself.

What must every index recommendation from this prompt include?

No index without a clause: each must name the exact predicates, joins, or sorts it serves, justify composite column order, and state its write tax, which inserts and updates it slows. INDEX RECOMMENDATIONS also checks existing indexes first, preferring to extend a near-miss. On the loaded query, the non-sargable YEAR(o.created_at) filter and SELECT * are the evidenced targets. You validate each on production-shaped data.

How does the prompt handle facts it can't verify from what I gave it?

Through the ASSUMPTIONS ledger: every assumption about data volumes, distributions, or index state is marked VERIFIED (with its evidence) or UNVERIFIED (with the query that would resolve it), and any recommendation depending on an UNVERIFIED assumption is flagged conditional. In Fast Review mode, pattern-level findings are labeled unverified with the check to confirm them. The tool structures this; confirming each assumption is your step.

More resources from SQL Optimization Prompt

Resources that pair well

Related tools

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