Prompt Library ⚙️ Technical SQL Query Performance Optimizer
GPT-4o ⚙️ Technical Advanced

SQL Query Performance Optimizer

Paste a slow SQL query with its schema and EXPLAIN output to get a diagnosis, optimized rewrite, and exact index recommendations.

👁 7 views ⎘ 0 copies ♥ 0 likes

The Prompt

# SQL Query Performance Optimizer

You are a database performance engineer. Analyze the slow query and schema provided, diagnose the issue, and produce an optimized version.

## Query Context

Database: [DATABASE_TYPE]
Table sizes: [TABLE_SIZES]
Current query execution time: [CURRENT_EXECUTION_TIME]
Target execution time: [TARGET_EXECUTION_TIME]

Schema (relevant tables):
[SCHEMA]

Slow query:
[SLOW_QUERY]

EXPLAIN output (if available):
[EXPLAIN_OUTPUT]

## Optimization Output

1. **Root cause** — why this query is slow (missing index, full table scan, N+1, Cartesian product, etc.)
2. **Optimized query** — rewritten query with the performance fix applied
3. **Index recommendations** — exact CREATE INDEX statements to run
4. **Expected EXPLAIN** — how the EXPLAIN output should look after optimization
5. **Trade-offs** — any downsides to the optimization (write overhead, storage cost, lock behavior)
6. **Further recommendations** — schema changes, query caching, or partitioning if relevant

📝 Fill in the blanks

Replace these placeholders with your own content:

[DATABASE_TYPE]
[TABLE_SIZES]
[CURRENT_EXECUTION_TIME]
[TARGET_EXECUTION_TIME]
[SCHEMA]
[SLOW_QUERY]
[EXPLAIN_OUTPUT]

How to use this prompt

1
Copy the prompt

Click "Copy Prompt" above to copy the full prompt text to your clipboard.

2
Replace the placeholders

Swap out anything in [BRACKETS] with your specific details.

3
Paste into GPT-4o

Open your preferred AI assistant and paste the prompt to get started.