Database Optimizer
Performance tuning, query optimization, and scalability across PostgreSQL and MySQL.
When to use
Slow queries, execution-plan analysis, index strategy, config tuning, schema/partitioning, lock contention, cache-hit improvement.
Core workflow
- Analyze — capture baseline metrics; run
EXPLAIN ANALYZE before any change.
- Identify bottlenecks — inefficient queries, missing indexes, config issues.
- Design solutions — index strategies, query rewrites, schema improvements.
- Implement — apply incrementally with monitoring; validate each change before the next.
- Validate — re-run
EXPLAIN ANALYZE, compare cost + wall-clock, document.
Always test in non-production first. Revert if write performance or replication lag worsens.
Common operations
-- Top slow queries (needs pg_stat_statements)
SELECT query, calls, round(mean_exec_time::numeric,2) AS mean_ms, rows
FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 20;
-- Capture plan with buffer cache ratio
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id
WHERE o.status = 'pending' AND o.created_at > now() - interval '7 days';
-- Covering index (eliminates heap fetch)
CREATE INDEX CONCURRENTLY idx_orders_status_created_covering
ON orders (status, created_at) INCLUDE (customer_id, total_amount);
Reading EXPLAIN — patterns:
| Pattern |
Symptom |
Remedy |
| Seq Scan on large table |
no filter selectivity |
add B-tree index |
| Nested Loop, large outer |
exponential inner growth |
Hash Join; index inner key |
| est rows=1, actual=50000 |
stale stats |
ANALYZE <table> |
| Buffers hit=10 read=90000 |
low cache hit |
bump shared_buffers; covering index |
| Sort Method: external merge |
sort spills to disk |
bump work_mem for session |
-- MySQL slow queries + plan
SELECT * FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC LIMIT 20;
EXPLAIN FORMAT=JSON SELECT * FROM orders WHERE status='pending';
Constraints
MUST: capture EXPLAIN (ANALYZE, BUFFERS) baseline first; measure before/after every change; create indexes CONCURRENTLY (PostgreSQL); test in non-prod and roll back on regression; document with before/after metrics; ANALYZE after bulk changes.
MUST NOT: optimize without a baseline; create redundant/unused indexes; make multiple changes at once; ignore write amplification from new indexes; neglect VACUUM/stats maintenance.
Output
- Analysis with baseline metrics (time, cost, buffer hit ratio)
- Bottlenecks + root causes (with EXPLAIN evidence)
- Optimization strategy
- SQL/config changes
- Validation queries
- Monitoring recommendations
1---2name: database-optimizer3description: "Use when investigating slow queries, analyzing execution plans, or optimizing database performance on PostgreSQL or MySQL. Covers index design, query rewrites, config tuning, partitioning, and lock contention. Triggers: database optimization, slow query, query performance, database tuning, index optimization, execution plan, EXPLAIN ANALYZE."4version: 1.0.05license: MIT6tags: [database, performance, postgresql, mysql, indexes, explain-analyze, tuning, query-optimization]7source: https://github.com/jeffallan/claude-skills/tree/main/skills/database-optimizer8derived_from: awesomeclaude9metadata:10 source: https://skillmd.com/skills/aashutosh396/database-optimizer11---1213# Database Optimizer1415Performance tuning, query optimization, and scalability across PostgreSQL and MySQL.1617## When to use1819Slow queries, execution-plan analysis, index strategy, config tuning, schema/partitioning, lock contention, cache-hit improvement.2021## Core workflow22231. **Analyze** — capture baseline metrics; run `EXPLAIN ANALYZE` before any change.242. **Identify bottlenecks** — inefficient queries, missing indexes, config issues.253. **Design solutions** — index strategies, query rewrites, schema improvements.264. **Implement** — apply incrementally with monitoring; validate each change before the next.275. **Validate** — re-run `EXPLAIN ANALYZE`, compare cost + wall-clock, document.2829Always test in non-production first. Revert if write performance or replication lag worsens.3031## Common operations3233```sql34-- Top slow queries (needs pg_stat_statements)35SELECT query, calls, round(mean_exec_time::numeric,2) AS mean_ms, rows36FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 20;3738-- Capture plan with buffer cache ratio39EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)40SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id41WHERE o.status = 'pending' AND o.created_at > now() - interval '7 days';4243-- Covering index (eliminates heap fetch)44CREATE INDEX CONCURRENTLY idx_orders_status_created_covering45 ON orders (status, created_at) INCLUDE (customer_id, total_amount);46```4748Reading EXPLAIN — patterns:49| Pattern | Symptom | Remedy |50|---|---|---|51| Seq Scan on large table | no filter selectivity | add B-tree index |52| Nested Loop, large outer | exponential inner growth | Hash Join; index inner key |53| est rows=1, actual=50000 | stale stats | `ANALYZE <table>` |54| Buffers hit=10 read=90000 | low cache hit | bump `shared_buffers`; covering index |55| Sort Method: external merge | sort spills to disk | bump `work_mem` for session |5657```sql58-- MySQL slow queries + plan59SELECT * FROM performance_schema.events_statements_summary_by_digest60ORDER BY SUM_TIMER_WAIT DESC LIMIT 20;61EXPLAIN FORMAT=JSON SELECT * FROM orders WHERE status='pending';62```6364## Constraints6566MUST: capture `EXPLAIN (ANALYZE, BUFFERS)` baseline first; measure before/after every change; create indexes `CONCURRENTLY` (PostgreSQL); test in non-prod and roll back on regression; document with before/after metrics; `ANALYZE` after bulk changes.67MUST NOT: optimize without a baseline; create redundant/unused indexes; make multiple changes at once; ignore write amplification from new indexes; neglect VACUUM/stats maintenance.6869## Output70711. Analysis with baseline metrics (time, cost, buffer hit ratio)722. Bottlenecks + root causes (with EXPLAIN evidence)733. Optimization strategy744. SQL/config changes755. Validation queries766. Monitoring recommendations