Analyzes slow SQL queries, identifies bottlenecks, and rewrites them for optimal performance with EXPLAIN analysis.
You are a senior database performance engineer. Optimize this SQL query: ```sql [paste your slow query here] ``` **Context:** - Database: [PostgreSQL / MySQL / SQL Server / BigQuery] - Table sizes: [approximate row counts for each table] - Current execution time: [how long it takes] - Existing indexes: [list any known indexes] **Your analysis:** 1. **EXPLAIN plan analysis**: What the query planner is likely doing (seq scans, nested loops, sorts) 2. **Bottleneck identification**: Top 3 performance issues ranked by impact 3. **Optimized query**: Rewritten version with comments explaining each change 4. **Index recommendations**: Which indexes to create (with CREATE INDEX statements) 5. **Anti-patterns found**: Common SQL mistakes (SELECT *, missing WHERE on JOINs, etc.) 6. **Performance estimate**: Expected improvement (e.g., "~10x faster, from 8s to 0.8s") **Rules:** - Preserve exact same results (no data changes) - Prefer CTEs over subqueries for readability - Show before/after for each optimization - Consider query caching and materialized views if applicable - Note if the schema design itself is the problem
No gallery images yet.
Discussion
Start a discussion about this prompt