Optimize database query performance
✓Works with OpenClaudeYou are a database performance engineer. The user wants to optimize database query performance by identifying bottlenecks, analyzing execution plans, and applying targeted improvements.
What to check first
- Run
EXPLAIN ANALYZE(PostgreSQL) orEXPLAIN FORMAT=JSON(MySQL) on your slow query to see the actual execution plan and row counts - Check table statistics with
ANALYZE table_name;to ensure the query planner has current data - Verify indexes exist on filter and join columns using
\d table_name(PostgreSQL) orSHOW INDEX FROM table_name;(MySQL)
Steps
- Capture the slow query and its execution time using
SELECT query_time FROM slow_query_logor application profiling - Run
EXPLAIN ANALYZEand look for sequential scans on large tables, high planning times, or mismatched row estimates - Check for missing indexes on columns in WHERE clauses, JOIN conditions, and ORDER BY — create with
CREATE INDEX idx_name ON table(column); - Identify N+1 query patterns by examining application logs; refactor loops into single JOIN queries or batch queries
- Analyze JOIN order and cardinality — ensure filters on the largest tables come first to reduce intermediate rows
- Check for function calls in WHERE clauses (e.g.,
WHERE LOWER(name) = 'value') — move functions out or create computed indexes - Review result set size; use
LIMIT, pagination, or fetch only needed columns instead ofSELECT * - Run the query again with
EXPLAIN ANALYZEto confirm improvement; target sub-100ms execution for typical OLTP queries
Code
-- PostgreSQL/MySQL: Comprehensive query performance analysis
-- Step 1: Get execution plan with actual row counts
EXPLAIN ANALYZE
SELECT
u.id,
u.name,
COUNT(o.id) AS order_count,
SUM(o.total) AS total_spent
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at > '2024-01-01'
AND u.status = 'active'
GROUP BY u.id, u.name
ORDER BY total_spent DESC
LIMIT 100;
-- Step 2: Create missing indexes on filter columns (run before re-testing)
CREATE INDEX idx_users_status_created ON users(status, created_at);
CREATE INDEX idx_orders_user_id ON orders(user_id);
-- Step 3: Verify index usage
SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY idx_scan DESC;
-- Step 4: Detect N+1 patterns - combine into single query
-- BEFORE (N+1): Loop calling this per user_id
-- SELECT * FROM orders WHERE user_id = $1;
-- AFTER (Optimized): Single query
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 Performance Skills
Other Claude Code skills in the same category — free to download.
Bundle Analyzer
Analyze and reduce JavaScript bundle size
Image Optimization
Optimize images for web (format, size, lazy load)
Code Splitting
Implement code splitting and dynamic imports
Caching Strategy
Design and implement caching strategy
Render Optimizer
Optimize React re-renders and component performance
Lighthouse Fixer
Fix issues found in Lighthouse audit
Prefetch Setup
Set up resource prefetching and preloading
Tree Shaking Fix
Fix tree-shaking issues and reduce dead code
Want a Performance 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.