Prompt Engineering SQL Execution Plans

Execution Plan Analysis — Read What the Engine Actually Chose

Optimize what the engine does, not what the SQL looks like: cost concentration, estimate-vs-actual gaps, and plan warnings — with forensic evidence rules.

Overview

Query text is the intention; the execution plan is the truth. This prompt uses the plan-analysis goal — work from the actual plan, find the one or two operators where the cost concentrates, compare estimates against actuals (large gaps mean stale statistics or unmodelable predicates, and invalidate downstream choices), and follow row counts between operators, because where rows explode or collapse is where the story is. The forensic mode makes it rigorous: every conclusion cites its operator, missing evidence becomes the exact command to gather it, and generic advice is forbidden. The loaded scenario — a settlement batch that tripled its runtime — starts where plan work usually starts: the plan not yet captured.

How to use this resource

  1. Capture before concluding

    No plan, no analysis: the first recommendation is the platform's capture command.

  2. Find the concentration

    One or two operators usually own the cost — the rest is noise to ignore.

  3. Trust actuals over estimates

    Where they diverge, fix the statistics story first — downstream choices depend on it.

Why This Works

  • Plan-first discipline optimizes reality instead of appearances
  • Cost concentration prevents effort spent on cheap operators
  • Estimate-vs-actual reading finds why the engine chose wrong, not just that it did

Best for

  • Queries whose text looks innocent and whose runtime is not
  • Batch jobs that degraded gradually over months
  • Engineers learning to read plans instead of guessing

Not for

  • Application-level performance hunting — that's the Debugging Prompt Generator
  • Plan-free quick passes — that's this tool's Fast Review evidence mode

Use cases

  • Setting up a rigorous plan review for a degrading batch job
  • Finding which operator actually owns the runtime
  • Turning missing plan evidence into capture commands

FAQ

How does the prompt handle a query when I haven't captured the execution plan yet?

It refuses to invent one. The QUERY CONTEXT section marks 'Execution plan: not provided. Do not invent one.' and instructs the output to state the exact capture command instead — the Actual Execution Plan plus SET STATISTICS IO, TIME ON — with every plan-dependent conclusion flagged pending that evidence. Missing evidence becomes a command to gather, never a guess.

Why does the prompt label conclusions as FACT, ASSUMPTION, or HYPOTHESIS?

That labeling comes from the Forensic Optimization evidence mode. Every statement is tagged FACT (from the provided query, plan, or schema), ASSUMPTION (believed without evidence), or HYPOTHESIS (a testable claim with its test), each recommendation carrying a confidence level tied to evidence strength. It's the rule that bars generic advice — anything that would read the same without seeing this query.

When the estimated and actual row counts diverge in my plan, what does the analysis do first?

It treats that gap as the lead signal, not a side note. Analysis priority 3 states a large estimate-vs-actual gap means stale statistics or unmodelable predicates and 'invalidates every downstream choice' — so cardinality gets resolved before any operator-cost reasoning built on top of it is trusted. The divergence sets the investigation order.

Can I use this same prompt for a slow application endpoint instead of a SQL query?

Treat it as SQL-plan-only. Everything is scoped to reading a database execution plan operator by operator — cost concentration, plan warnings like CONVERT_IMPLICIT and spills, Key Lookup executions. Its NON-GOALS section puts application-level changes out of scope entirely, and schema redesign too unless the evidence shows no query-level fix exists — so an app endpoint needs a different prompt.

More resources from SQL Optimization Prompt

Resources that pair well

Related tools

Guides for this resource

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