Browse
Data

SQL query review and speed-up

Have a slow SQL query checked for wrong results first and speed second. You get a rewrite, the indexes to add, and a way to prove it's faster.

No votes yet this week
Output
Text
Written with
Claude Fable 5.1
Added
Updated

The prompt

Review this SQL query the way a database performance engineer would.

Database and version: [e.g. PostgreSQL 16]
Rough table sizes: [e.g. orders 40 million rows, customers 2 million]
Indexes that exist: [LIST THEM, OR PASTE THE OUTPUT OF \d tablename]
What it should return, in plain words: [DESCRIPTION]

```sql
[PASTE THE QUERY]
```

EXPLAIN ANALYZE output, if you have it:
```
[PASTE]
```

In this order:
1. Correctness first. Can it ever return wrong or duplicated rows? Think about NULLs, joins that multiply rows, and time zones.
2. What makes it slow? Plain language, please.
3. Rewrite it, and tell me what you assumed.
4. Give me the exact CREATE INDEX statements. If an existing index isn't helping, say so.
5. How do I check that it really got faster?
  • sql
  • postgres
  • performance

What it generates

Example

NoteThis is an example. Results change between runs and model versions. Claude Prompts is a fan project and is not affiliated with Anthropic.

Getting the most from this prompt

Good for

A query that crawls in production, a report whose totals look slightly off, or a last look before merging something that touches a big table. It's written with PostgreSQL in mind. MySQL and SQL Server are fine too, as long as you name the engine and version.

The details that change the advice

Table sizes and existing indexes matter a lot, so don't skip them. An index that saves you on 40 million rows is dead weight on 4,000. If you can get the EXPLAIN ANALYZE output, paste it, because without it Claude is reasoning about a likely plan and not your real one. For reports, describe the result you expect in plain words. That's how double counting gets caught.

Before you run anything in production

Correctness comes first in the prompt for a reason: a fast query with wrong numbers is worse than a slow one with right ones. Try new indexes on a copy of the data, and make sure an index really is unused before you drop it. On a live PostgreSQL database, build with CONCURRENTLY so you don't block writes.

How to use this prompt

  1. [01] Copy

    Copy the prompt

    Use the copy button above. The full text is included, nothing is cut off.

  2. [02] Fill in

    Replace the yellow parts

    Everything highlighted in yellow is a placeholder. Swap it for your own topic, audience or data.

  3. [03] Vote

    Run it in Claude and vote

    If the result helped, press the heart. Votes decide which prompts make the weekly top 10.

More like this

All data prompts
Image outputA bar chart of monthly newsletter signups from January to December, with the launch month highlighted and labelled

Turn a CSV into a clean chart

Paste raw CSV and get a clean SVG chart with sensible scales, labels on the data, and a title that says what the chart shows.

Video outputAn animated chart building up a comparison of two bars over a few seconds, with a counter ticking up to the final figure and a caption stating the finding

Animate a number so people feel it

Give Claude one statistic and its context, and get a short animated visual in HTML that builds the number up step by step instead of just showing it.

Learn the technique

All articles
2 min read

How the weekly ranking works

One vote per prompt, a fresh count every Monday, and a simple tie-break. Everything about how prompts reach the top three, in plain words.

2 min read

How to use XML tags in Claude prompts

Wrapping parts of your prompt in simple tags is one of the easiest ways to get more reliable answers from Claude. A practical guide with examples.