Slow Query Review
Analyse a slow SQL query and its execution plan to find the likely causes and the changes worth testing.
When to use it
Use it when a specific query is slow and you have its text, the table definitions and the execution plan. The prompt reads the plan, locates the expensive steps and proposes changes ranked by likely benefit and risk, each with a way to measure whether it helped.
When not to use it: Do not use it without the execution plan from your own database: guesses about performance without a plan are unreliable. It cannot replace measurement, and index suggestions must not be applied to production without testing. Use synthetic or redacted data in anything you paste.
Required inputs
| Placeholder | What to provide | Example |
|---|---|---|
| {{query}} | The SQL text, with parameter values replaced by realistic placeholders. | SELECT o.id, o.total FROM orders o JOIN customers c ON c.id = o.customer_id WHERE c.region = 'north' AND o.created_at >= '2026-10-01' ORDER BY o.created_at DESC LIMIT 50; |
| {{schema}} | Table definitions for the tables involved, including existing indexes and approximate row counts. | orders(id pk, customer_id fk, total, created_at) about 12M rows, index on customer_id only. customers(id pk, region) about 300k rows. |
| {{execution_plan}} | The output of your database's execution plan command with actual timings if available. Include the engine and version. | PostgreSQL 15 EXPLAIN (ANALYZE, BUFFERS) output showing a sequential scan on orders. |
| {{constraints}} | Limits on what can change: indexes you cannot add, queries owned by another team, write-heavy tables. | orders is write-heavy; every new index has a cost. Query text can change. |
The prompt
Fill in each {{placeholder}} before you send it.
You are a database performance engineer. Analyse this slow query using the execution plan provided. Base everything on the plan and schema below.
<query>
{{query}}
</query>
<schema>
{{schema}}
</schema>
<execution_plan>
{{execution_plan}}
</execution_plan>
<constraints>
{{constraints}}
</constraints>
Do the following.
1. Read the plan from the innermost step outwards. Identify the steps that take the most time or process the most rows, and where the estimated and actual row counts differ sharply.
2. Explain in plain language why each expensive step is expensive, referring to specific plan nodes.
3. List candidate changes, each with: what to change (query rewrite, index, statistics, schema), the expected effect and why, the cost or risk (for example write overhead of a new index), and how to measure whether it worked.
4. Rank candidates by expected benefit relative to risk, and say which to try first.
Rules:
- Refer only to what the plan and schema show. If the plan was run without actual timings, say that the conclusions are weaker.
- Do not promise a speed-up or quote improvement figures. Say what you expect to change in the plan, not how many milliseconds you expect to save.
- Do not suggest an index without stating its columns, column order and the reason for the order, and without noting the effect on writes.
- Do not assume engine features. If a suggestion depends on a feature that may be version-specific, say so and tell me what to verify.
- Preserve the query's results. If a rewrite could change results, say how.
Output format:
Reading of the plan
Why it is slow
Candidate changes (table: change | effect on the plan | cost or risk | how to measure)
Recommended first step
What to verify before applying anythingExpected output
A walk through the plan naming the expensive nodes, an explanation of why they are costly, and a ranked table of candidate changes with their cost and a way to measure each. For the example, a sequential scan on a large table with a sort should lead to a composite index suggestion with its column order reasoned, a note on write overhead, and a comparison of the plan before and after. It does not quote invented speed-ups.
Reading of the plan
The sequential scan on orders reads all 12 million rows and discards most of them, then sorts what remains before applying LIMIT 50. The join to customers is not the main cost.
Candidate changes
1. Index on orders (created_at DESC) | May let the planner read recent rows in order and stop after 50, avoiding the sort | Extra write cost on every insert to a write-heavy table | Compare plans with EXPLAIN (ANALYZE, BUFFERS); check the sort node has gone
2. Update table statistics | Estimated and actual rows differ sharply on the customers filter, so better statistics may change the join order | Low | Rerun the plan and compare estimates with actuals
Recommended first step
Refresh statistics (low risk), then test the index on a copy of the data.Limitations
- Plans vary with data size, distribution, statistics and configuration. A suggestion that helps on one dataset may do nothing on another.
- The assistant cannot run the query. It reasons about a plan snapshot that may not reflect production conditions such as caching.
- New indexes slow writes and use storage. It may understate this cost.
- Plan formats and available options differ between engines and versions, so named features need checking.
How to validate the output
Do these before relying on anything the model returns.
- Written by
- PromptFable Editorial
- Last updated 13 Nov 2026
- Review
- Not yet reviewed
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.