CLSkills
Software Development

How to Use Claude for Database Optimization

The exact prompt, setup, and codes to get great results.

The Problem

Slow queries kill user experience and server costs. Most developers know 'add an index' but don't know how to read query plans or restructure schemas for performance.

The Prompt (Copy & Paste)

Optimize this database query/schema.

Database: [POSTGRES / MYSQL / MONGODB]
Table sizes: [APPROXIMATE ROW COUNTS]
Current query time: [HOW SLOW]
Target query time: [WHAT'S ACCEPTABLE]

Schema:
```sql
[PASTE CREATE TABLE STATEMENTS OR SCHEMA]
```

Slow query:
```sql
[PASTE THE QUERY]
```

EXPLAIN output (if available):
```
[PASTE EXPLAIN ANALYZE OUTPUT]
```

Analyze:
1. What's causing the slowness (sequential scan, missing index, bad join order, N+1)?
2. Show the optimized query
3. Suggest index additions (with CREATE INDEX statements)
4. Flag any schema changes that would help at scale
5. Estimate the improvement and any tradeoffs (write speed, storage)

Replace the [BRACKETED] placeholders with your specific details.

What You Get

A clear diagnosis of why your query is slow, with ready-to-run SQL for indexes and query rewrites. Includes tradeoff analysis so you understand the cost of each optimization.

Prompt Codes That Help

Add these prefix codes to the start of your prompt for even better results:

Click any code to see its before/after examples and learn how it works.

Want all 120+ prompt codes?

The Claude Prompt Cheat Sheet has every tested code with before/after examples, combo stacks, and 10 workflow playbooks for different roles.

More Software Development Tasks

πŸ“¬

Get new prompts + workflows weekly

Join developers and professionals getting tested Claude prompt codes, workflows, and real-world techniques. One email when there’s something worth sharing.