SQL Query Optimizer

Explains why a slow SQL query is likely slow and proposes a rewritten version plus relevant indexes.

Data Engineering and Analyticsintermediate8 minNo ratings yet

When to use this

Use when a specific query is slow and you have (or can get) its execution plan or schema.

Prompt template

You are a database performance specialist for {{database_engine}}. Slow query:

```sql
{{query}}
```

Relevant schema/indexes: {{schema_context}}
Execution plan or observed symptom, if known: {{execution_plan}}

Provide:
1. The most likely cause(s) of slowness (e.g. missing index, full scan, non-sargable predicate, N+1 pattern).
2. A rewritten query if a rewrite would help, with what changed.
3. Index(es) to add or change, with the exact DDL.
4. What to re-check after the change to confirm it actually helped.

Customize

Slow query is required

Preview

You are a database performance specialist for PostgreSQL. Slow query:

```sql
{{query}} missing
```

Relevant schema/indexes: 
Execution plan or observed symptom, if known: 

Provide:
1. The most likely cause(s) of slowness (e.g. missing index, full scan, non-sargable predicate, N+1 pattern).
2. A rewritten query if a rewrite would help, with what changed.
3. Index(es) to add or change, with the exact DDL.
4. What to re-check after the change to confirm it actually helped.

Usage instructions

Include the schema (even just column names and existing indexes) for a specific recommendation; without it, the answer stays generic about likely causes.

Example output

Diagnoses the sequential scan as caused by a missing index on customer_id, suggests `CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at DESC);`, and recommends re-running EXPLAIN ANALYZE to confirm an index scan replaces the seq scan.

Limitations

Index recommendations assume the query pattern shown is representative; adding indexes has write-performance tradeoffs this prompt does not evaluate.

Rate this prompt

Not yet rated