SQL Query Optimizer
Diagnose and rewrite slow SQL queries with expert-level explanations.
Use case
Analyzing and optimizing SQL queries for better performance
Works best with
Input needed
The SQL query, table schemas (with indexes), approximate row counts, and the database engine (PostgreSQL, MySQL, etc.)
Output format
Optimized query, explanation of changes, index recommendations, and estimated performance impact
Example use
Paste a slow JOIN query across 3 tables. Get back an optimized version using proper indexing, query restructuring, and an EXPLAIN plan analysis showing why the original was slow.
Editor’s Note
Highly reliable for common optimization patterns (missing indexes, N+1 queries, subquery-to-JOIN rewrites). Less reliable for database-specific edge cases and optimizer hints. Always test with EXPLAIN ANALYZE on your actual data — row estimates and cardinality matter.
Full Prompt
Act as a senior database administrator and SQL performance expert. Analyze and optimize the following SQL query. ```sql [PASTE QUERY HERE] ``` Database: [PostgreSQL / MySQL / SQLite / other] Table sizes (approximate): [e.g., users: 2M rows, orders: 10M rows] Provide: 1. Diagnosis — Explain what is causing the slowness (full table scan, missing index, N+1, etc.). 2. Optimized Query — Rewrite the query for maximum performance. 3. Indexes to Create — Exact CREATE INDEX statements to add. 4. Explanation — Why each change improves performance.
Tags
