PlaybookPrompts

Diagnose a slow SQL query using EXPLAIN output

Coding & Development sqlperformancedatabase

When a query is running slow and you have an EXPLAIN or EXPLAIN ANALYZE output, it's not always obvious which node is the culprit. This prompt helps you interpret the plan and get concrete index or rewrite suggestions.

Prompt
You are a database performance specialist. I have a SQL query that is running too slowly in production and I have its execution plan.

Database engine: {{DB_ENGINE}} (e.g. PostgreSQL 15, MySQL 8, SQLite)

Original query:
```sql
{{SLOW_QUERY}}
```

EXPLAIN / EXPLAIN ANALYZE output:
```
{{EXPLAIN_OUTPUT}}
```

Table sizes and relevant context: {{TABLE_CONTEXT}}

Please do the following:

1. Identify the single most expensive node in the execution plan and explain in plain English why it is expensive.
2. Check for sequential scans on large tables. For each one, state whether an index could help and what columns that index should cover.
3. Check for nested loop joins on large row sets. If present, suggest whether a hash join or merge join would be more appropriate and why.
4. List up to three concrete, prioritized changes (index additions, query rewrites, or config changes) ordered by expected impact.
5. For each suggestion, write the exact SQL or config change needed.
6. Note any suggestions that require a maintenance window or could lock the table in production.

Do not suggest changes that require upgrading the database version unless you flag them clearly as version-dependent.
Variables to fill in
  • {{DB_ENGINE}}
  • {{SLOW_QUERY}}
  • {{EXPLAIN_OUTPUT}}
  • {{TABLE_CONTEXT}}

How to use this prompt

  1. Copy the prompt above (Copy button on the top-right).
  2. Replace each {{VAR}} with your own value. Variables: {{DB_ENGINE}}{{SLOW_QUERY}}{{EXPLAIN_OUTPUT}}{{TABLE_CONTEXT}}.
  3. Paste it into one of the recommended tools below.
  4. Iterate: tighten constraints in the prompt if the output is generic.

Why this prompt is structured this way

The prompt is split into explicit steps because LLMs do better when the path is named, not implied. Each variable forces specificity at the input layer — vague inputs get vague outputs.

Heads up: some of the links on this page are affiliate links — meaning we may earn a commission if you sign up after clicking, at no extra cost to you. We only recommend tools we'd put on our own stack. You can see our full affiliate disclosure here.

Pair this prompt with a tool

The PlaybookPrompts weekly

One short email per week. The five sharpest prompts we found, one tool worth your attention, one anti-pattern to avoid. Unsubscribe in one click.

Newsletter signup is not configured. Set PUBLIC_NEWSLETTER_USERNAME in the build env.