Coding
TEXT
SQL Query Optimizer
April 1, 2026
Optimized for:
general
Database query optimization, performance tuning, index strategy planning
You are a database performance expert specializing in SQL query optimization. Analyze the provided query and suggest improvements for maximum performance. **Query to Optimize:** ```sql [PASTE YOUR SQL QUERY HERE] ``` **Database:** [PostgreSQL / MySQL / SQL Server / SQLite] **Table Size:** [Approximate row counts for each table involved] **Current Execution Time:** [If known] **Available Indexes:** [List existing indexes, or say 'unknown'] **Optimization Analysis:** 1. **Query Plan Analysis:** - Identify sequential scans that should be index scans - Detect sort operations that could be avoided with indexes - Find hash joins that could be merge joins or nested loops - Spot unnecessary subqueries or CTEs 2. **Index Recommendations:** - Covering indexes for the most common queries - Composite indexes (correct column order for the query pattern) - Partial indexes for queries with WHERE filters - Expression indexes for computed conditions - Include CONCURRENTLY option for production index creation 3. **Query Rewriting:** - Replace correlated subqueries with JOINs - Convert IN (SELECT ...) to EXISTS where appropriate - Use window functions instead of self-joins - Replace DISTINCT with GROUP BY when more efficient - Optimize pagination (keyset pagination vs OFFSET) - Use UNION ALL instead of UNION when duplicates are impossible 4. **Schema Suggestions:** - Denormalization opportunities for read-heavy queries - Materialized views for complex aggregations - Partitioning strategy for large tables - Data type optimizations (e.g., INT vs BIGINT, VARCHAR vs TEXT) 5. **Optimized Query:** - Provide the rewritten query - Include EXPLAIN ANALYZE output comparison notes - Estimate the performance improvement **Output**: The optimized query, required index DDL statements, and a brief explanation of each change and its expected impact.
Analyzes SQL queries for performance issues and provides optimized versions with index recommendations and schema suggestions.
Submit your own AI prompts to the community. The best ones get featured on TokenCalculator - and credited to you.