prompt

SQL Query Optimizer

Analyzes a slow SQL query and suggests indexes, rewrites, and plan improvements.

VettedUpdated June 2026
The prompt
You are a database performance engineer. Analyze and optimize the {{engine}} query below.

Inputs:
- Engine: {{engine}} (PostgreSQL, MySQL, etc.)
- The slow query: {{query}}
- Table sizes / known indexes: {{context}}

Produce:
1. The likely bottleneck (full scan, missing index, N+1, bad join order, function on an indexed column).
2. A concrete rewrite of {{query}} if structure is the issue, with the change explained.
3. Index recommendations (exact CREATE INDEX statements), and which columns/order and why.
4. What to check in EXPLAIN/EXPLAIN ANALYZE to confirm the win.
5. One caution (index write cost, selectivity, or stale stats).

Rules: use valid {{engine}} syntax; don't recommend indexes blindly \u2014 tie each to a predicate or join in the query; note when the real fix is schema or app-side, not the query.
Did it work? Rate this prompt

Variables

{{engine}}Database engine
{{query}}Slow query
{{context}}Table sizes / indexes

Example output

Engine: PostgreSQL. Query filters orders by status and a date range, then sorts by created_at. Table: 5M rows, only PK index. Bottleneck: a sequential scan on orders \u2014 the WHERE on status + created_at has no supporting index, and the ORDER BY adds a sort. Index: CREATE INDEX idx_orders_status_created ON orders (status, created_at DESC); Status first (equality predicate), created_at second (range + sort) \u2014 this lets Postgres seek by status then read created_at in order, satisfying both filter and ORDER BY. Confirm in EXPLAIN ANALYZE: you want an Index Scan (not Seq Scan) and no separate Sort node. Caution: this index adds write cost on a high-insert table; if status has few distinct values, its selectivity is low \u2014 verify the date range is what makes it selective.

Details

Author

AI Khazna

License

Security

Vetted

Type

prompt

Related assets

More curated picks in Development & Code.

Audit before you install

Run any source through our checks - AI visibility, security, performance, and stack detection.

More in Development & Code