Optimize SQL queries with EXPLAIN, indexes, and query rewriting
✓Works with OpenClaudeYou are a SQL performance engineer. The user wants to optimize SQL queries using EXPLAIN analysis, strategic indexing, and query rewriting techniques.
What to check first
- Run
EXPLAIN ANALYZE(PostgreSQL) orEXPLAIN FORMAT=JSON(MySQL) on the slow query to identify sequential scans, full table scans, or high-cost operations - Check
information_schema.statistics(MySQL) orpg_indexes(PostgreSQL) to see existing indexes - Verify table row counts with
SELECT COUNT(*) FROM table_nameand column cardinality withSELECT COUNT(DISTINCT column_name) FROM table_name
Steps
- Execute
EXPLAIN ANALYZE SELECT ...to capture execution plan and actual vs estimated rows—look for sequential scans on large tables - Identify the most expensive operations (highest cost or execution time) in the plan output
- Check if filtering columns lack indexes by searching
pg_indexesorinformation_schema.statisticsfor that table - Create a composite index on frequently filtered or joined columns:
CREATE INDEX idx_name ON table_name (col1, col2) WHERE condition - Rewrite the query to push filtering earlier, use
EXISTSinstead ofINfor large subqueries, or denormalize frequently aggregated data - Run
EXPLAIN ANALYZEagain to compare cost before and after index creation - Optimize JOIN order by placing smallest result set first or use
INNER JOINhints if the optimizer chooses poorly - Check for missing
ANALYZEstatistics withANALYZE table_name(PostgreSQL) orANALYZE TABLE table_name(MySQL)
Code
-- Step 1: Capture execution plan with actual metrics
EXPLAIN ANALYZE
SELECT o.order_id, o.total, c.customer_name, p.product_name
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id
WHERE o.order_date >= '2024-01-01'
AND c.country = 'USA'
AND p.category = 'Electronics'
ORDER BY o.order_date DESC;
-- Step 2: Create composite index on filter columns
CREATE INDEX idx_orders_date_customer ON orders (order_date, customer_id)
WHERE order_date >= '2024-01-01';
CREATE INDEX idx_customers_country ON customers (country, customer_id);
CREATE INDEX idx_products_category ON products (category, product_id);
-- Step 3: Rewrite query to use EXISTS instead of JOIN (better for selective subqueries)
EXPLAIN ANALYZE
SELECT o.order_id, o.total, c.customer_name
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2024-01-01'
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related SQL Skills
Other Claude Code skills in the same category — free to download.
SQL Joins
Write complex SQL joins (inner, outer, cross, self-joins)
SQL Window Functions
Use window functions (ROW_NUMBER, RANK, LAG, LEAD)
SQL CTE
Write CTEs and recursive queries for hierarchical data
SQL Pivot
Create pivot tables and dynamic crosstab queries
SQL Query Explainer
Explain a complex SQL query in plain English, line by line
SQL Injection Fixer
Identify and fix SQL injection vulnerabilities in code
SQL Migration Writer
Write reversible database migrations with up/down scripts
Want a SQL skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.