Prompt Library › ⚙️ Technical › Optimize Slow Database Queries
GPT-4o ⚙️ Technical Intermediate

Optimize Slow Database Queries

Paste a slow [DATABASE_TYPE] query and get AI to rewrite it with better indexing strategy, join optimization, and a clear explanation of each change.

👁 6 views ⎘ 0 copies ♥ 0 likes

The Prompt

# Optimize Slow Database Queries

You are a database performance engineer. Optimize the following [DATABASE_TYPE] query for [TABLE_NAME] which is taking [CURRENT_DURATION]ms.

## Current Query

```sql
[PASTE_SLOW_QUERY_HERE]
```

## Table Statistics

- **Row count:** [ROW_COUNT]
- **Existing indexes:** [INDEX_LIST]
- **Query frequency:** [CALLS_PER_MINUTE] calls/minute
- **Database version:** [DB_VERSION]

## EXPLAIN Output

```
[PASTE_EXPLAIN_OUTPUT_HERE]
```

## Optimization Goals

1. Reduce execution time to under [TARGET_MS]ms
2. Reduce full table scans on [TABLE_NAME]
3. Support [CONCURRENT_CONNECTIONS] concurrent connections

## Output

1. Optimized query with comments explaining each change
2. Recommended index definitions with CREATE INDEX statements
3. Before/after EXPLAIN comparison
4. Any schema changes that would help (with migration steps)

📝 Fill in the blanks

Replace these placeholders with your own content:

[DATABASE_TYPE]
[TABLE_NAME]
[CURRENT_DURATION]
[PASTE_SLOW_QUERY_HERE]
[ROW_COUNT]
[INDEX_LIST]
[CALLS_PER_MINUTE]
[DB_VERSION]
[PASTE_EXPLAIN_OUTPUT_HERE]
[TARGET_MS]
[CONCURRENT_CONNECTIONS]

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.