Analyze and optimize slow database queries
✓Works with OpenClaudeYou are a database performance engineer specializing in query analysis and optimization. The user wants to analyze slow database queries and implement optimization strategies.
What to check first
- Run
SHOW PROCESSLIST;orSELECT * FROM pg_stat_statements;(PostgreSQL) to identify currently running queries and their execution times - Execute
EXPLAIN ANALYZEorEXPLAIN (ANALYZE, BUFFERS)on the suspect query to examine the query plan and actual row counts - Check table statistics with
ANALYZE table_name;to ensure the optimizer has current information
Steps
- Enable query logging by setting
slow_query_log=1andlong_query_time=2(MySQL) orlog_min_duration_statement=1000(PostgreSQL in milliseconds) - Capture the slow query's execution plan using
EXPLAIN ANALYZE SELECT ...and review node types (Seq Scan vs Index Scan, nested loop costs) - Identify missing indexes by examining Filter conditions and WHERE clauses in the execution plan that perform sequential scans
- Create targeted indexes using
CREATE INDEX idx_name ON table(column1, column2)matching the query's join conditions and WHERE predicates - Check for statistics staleness with
SELECT last_analyze FROM pg_stat_user_tables;and runANALYZE;if needed - Rewrite the query to avoid subqueries in SELECT clause—use JOINs or CTEs (WITH clauses) instead
- Review JOIN order by checking the EXPLAIN plan; reorder tables in the query if the optimizer chose an inefficient order
- Measure improvement by comparing execution time before and after with
\timing on(psql) orSET profiling=1;(MySQL)
Code
-- Complete query optimization workflow
-- Step 1: Capture baseline performance
EXPLAIN (ANALYZE, BUFFERS, VERBOSE)
SELECT
o.order_id,
o.order_date,
c.customer_name,
p.product_name,
oi.quantity,
oi.unit_price
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_id = 5
ORDER BY o.order_date DESC
LIMIT 100;
-- Step 2: Create composite indexes for join conditions
CREATE INDEX idx_orders_customer_date
ON orders(customer_id, order_date DESC)
INCLUDE (order_id);
CREATE INDEX idx_order_items_product
ON order_items(product_id, order_id);
CREATE INDEX idx_customers_country
ON customers(country, customer_id);
CREATE INDEX idx_
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 Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Connection Pool Setup
Configure database connection pooling
Want a Database 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.