Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Performanceadvanced

Database Query Perf

Share

Optimize database query performance

Works with OpenClaude

You 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) or EXPLAIN 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) or SHOW INDEX FROM table_name; (MySQL)

Steps

  1. Capture the slow query and its execution time using SELECT query_time FROM slow_query_log or application profiling
  2. Run EXPLAIN ANALYZE and look for sequential scans on large tables, high planning times, or mismatched row estimates
  3. Check for missing indexes on columns in WHERE clauses, JOIN conditions, and ORDER BY — create with CREATE INDEX idx_name ON table(column);
  4. Identify N+1 query patterns by examining application logs; refactor loops into single JOIN queries or batch queries
  5. Analyze JOIN order and cardinality — ensure filters on the largest tables come first to reduce intermediate rows
  6. Check for function calls in WHERE clauses (e.g., WHERE LOWER(name) = 'value') — move functions out or create computed indexes
  7. Review result set size; use LIMIT, pagination, or fetch only needed columns instead of SELECT *
  8. Run the query again with EXPLAIN ANALYZE to 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

Quick Info

CategoryPerformance
Difficultyadvanced
Version1.0.0
AuthorClaude Skills Hub
performancedatabasequeries

Install command:

curl -o ~/.claude/skills/database-query-perf.md https://claude-skills-hub.vercel.app/skills/performance/database-query-perf.md

Related Performance Skills

Other Claude Code skills in the same category — free to download.

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.