Browse
Data

SQL Query Review and Speed-Up

Get a slow SQL query checked for wrong results first and speed second, with a rewrite, the exact indexes to add and a way to verify the gain.

No votes yet this week
Output
Text
Added
Updated

The prompt

Review this SQL query like a database performance engineer would.

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

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

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

In this order:
1. Correctness first. Is there any case where it returns wrong or duplicated rows? Think about NULLs, joins that multiply rows, and time zones.
2. Explain what makes it slow, in plain language.
3. Rewrite it, and state any assumptions you made.
4. Give the exact CREATE INDEX statements to add. Mention any existing index that is not helping.
5. Tell me how to check that it really got faster.
  • sql
  • postgres
  • performance

What it generates

Example

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

Getting the most from this prompt

When to use it

A query that is slow in production, a report whose totals look slightly off, or a review before you merge a query that touches a large table. It is written with PostgreSQL in mind, and works for MySQL and SQL Server when you name the engine and version.

Make it your own

  • Table sizes and existing indexes change the advice completely, so include them. An index that helps on 40 million rows is wasted on 4,000.
  • Paste the EXPLAIN ANALYZE output when you have it. Without it, Claude is reasoning about likely plans, not the real one.
  • For reports, describe the expected result in plain words. That is how the double counting bugs get caught.

Watch out for

The correctness check comes first on purpose: a fast query that returns wrong numbers is worse than a slow one. Try suggested indexes on a copy of the data, and check that an index is unused before you drop it. Use CONCURRENTLY on a live PostgreSQL database so that you do not 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 data and get a publication-ready SVG chart with sensible scales, direct labels and a title that states the finding.

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.