{"data":{"slug":"discovered-42fd54432e26a35a899133a1a3ed05c0e35c898a2367d5b6","title":"Database Optimizer","summary":"Optimizes database queries and improves performance across PostgreSQL and MySQL systems. Use when investigating slow queries, analyzing execution plans, or optimizing database performance. Deliverable includes analysis and code for index design, query rewrites, configuration tuning, partitioning strategies, and lock contention resolution.","url":"https:\/\/skillshunter.net\/skills\/discovered-42fd54432e26a35a899133a1a3ed05c0e35c898a2367d5b6","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-42fd54432e26a35a899133a1a3ed05c0e35c898a2367d5b6","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-42fd54432e26a35a899133a1a3ed05c0e35c898a2367d5b6.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-42fd54432e26a35a899133a1a3ed05c0e35c898a2367d5b6\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before applying any optimizations, capture `EXPLAIN (ANALYZE, BUFFERS)` output as a baseline. Always test changes in non-production environments and revert immediately if write performance degrades or replication lag increases.","instructions":"---\nname: database-optimizer\ndescription: Optimizes database queries and improves performance across PostgreSQL and MySQL systems. Use when investigating slow queries, analyzing execution plans, or optimizing database performance. Invoke for index design, query rewrites, configuration tuning, partitioning strategies, lock contention resolution.\nlicense: MIT\nmetadata:\n  author: https:\/\/github.com\/Jeffallan\n  company: https:\/\/synergetic.solutions\n  version: \"1.1.1\"\n  domain: infrastructure\n  triggers: database optimization, slow query, query performance, database tuning, index optimization, execution plan, EXPLAIN ANALYZE, database performance, PostgreSQL optimization, MySQL optimization\n  role: specialist\n  scope: optimization\n  output-format: analysis-and-code\n  related-skills: devops-engineer, postgres-pro, graphql-architect\n---\n\n# Database Optimizer\n\nSenior database optimizer with expertise in performance tuning, query optimization, and scalability across multiple database systems.\n\n## When to Use This Skill\n\n- Analyzing slow queries and execution plans\n- Designing optimal index strategies\n- Tuning database configuration parameters\n- Optimizing schema design and partitioning\n- Reducing lock contention and deadlocks\n- Improving cache hit rates and memory usage\n\n## Core Workflow\n\n1. **Analyze Performance** \u2014 Capture baseline metrics and run `EXPLAIN ANALYZE` before any changes\n2. **Identify Bottlenecks** \u2014 Find inefficient queries, missing indexes, config issues\n3. **Design Solutions** \u2014 Create index strategies, query rewrites, schema improvements\n4. **Implement Changes** \u2014 Apply optimizations incrementally with monitoring; validate each change before proceeding to the next\n5. **Validate Results** \u2014 Re-run `EXPLAIN ANALYZE`, compare costs, measure wall-clock improvement, document changes\n\n> \u26a0\ufe0f Always test changes in non-production first. Revert immediately if write performance degrades or replication lag increases.\n\n## Reference Guide\n\nLoad detailed guidance based on context:\n\n| Topic | Reference | Load When |\n|-------|-----------|-----------|\n| Query Optimization | `references\/query-optimization.md` | Analyzing slow queries, execution plans |\n| Index Strategies | `references\/index-strategies.md` | Designing indexes, covering indexes |\n| PostgreSQL Tuning | `references\/postgresql-tuning.md` | PostgreSQL-specific optimizations |\n| MySQL Tuning | `references\/mysql-tuning.md` | MySQL-specific optimizations |\n| Monitoring & Analysis | `references\/monitoring-analysis.md` | Performance metrics, diagnostics |\n\n## Common Operations & Examples\n\n### Identify Top Slow Queries (PostgreSQL)\n```sql\n-- Requires pg_stat_statements extension\nSELECT query,\n       calls,\n       round(total_exec_time::numeric, 2)  AS total_ms,\n       round(mean_exec_time::numeric, 2)   AS mean_ms,\n       round(stddev_exec_time::numeric, 2) AS stddev_ms,\n       rows\nFROM   pg_stat_statements\nORDER  BY mean_exec_time DESC\nLIMIT  20;\n```\n\n### Capture an Execution Plan\n```sql\n-- Use BUFFERS to expose cache hit vs. disk read ratio\nEXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)\nSELECT o.id, c.name\nFROM   orders o\nJOIN   customers c ON c.id = o.customer_id\nWHERE  o.status = 'pending'\n  AND  o.created_at > now() - interval '7 days';\n```\n\n### Reading EXPLAIN Output \u2014 Key Patterns to Find\n\n| Pattern | Symptom | Typical Remedy |\n|---------|---------|----------------|\n| `Seq Scan` on large table | High row estimate, no filter selectivity | Add B-tree index on filter column |\n| `Nested Loop` with large outer set | Exponential row growth in inner loop | Consider Hash Join; index inner join key |\n| `cost=... rows=1` but actual rows=50000 | Stale statistics | Run `ANALYZE <table>;` |\n| `Buffers: hit=10 read=90000` | Low buffer cache hit rate | Increase `shared_buffers`; add covering index |\n| `Sort Method: external merge` | Sort spilling to disk | Increase `work_mem` for the session |\n\n### Create a Covering Index\n```sql\n-- Covers the filter AND the projected columns, eliminating a heap fetch\nCREATE INDEX CONCURRENTLY idx_orders_status_created_covering\n    ON orders (status, created_at)\n    INCLUDE (customer_id, total_amount);\n```\n\n### Validate Improvement\n```sql\n-- Before optimization: save plan & timing\nEXPLAIN (ANALYZE, BUFFERS) <query>;   -- note \"Execution Time: X ms\"\n\n-- After optimization: compare\nEXPLAIN (ANALYZE, BUFFERS) <query>;   -- target meaningful reduction in cost & time\n\n-- Confirm index is actually used\nSELECT indexname, idx_scan, idx_tup_read, idx_tup_fetch\nFROM   pg_stat_user_indexes\nWHERE  relname = 'orders';\n```\n\n### MySQL: Find Slow Queries\n```sql\n-- Inspect slow query log candidates\nSELECT * FROM performance_schema.events_statements_summary_by_digest\nORDER  BY SUM_TIMER_WAIT DESC\nLIMIT  20;\n\n-- Execution plan\nEXPLAIN FORMAT=JSON\nSELECT * FROM orders WHERE status = 'pending' AND created_at > NOW() - INTERVAL 7 DAY;\n```\n\n## Constraints\n\n### MUST DO\n- Capture `EXPLAIN (ANALYZE, BUFFERS)` output **before** optimizing \u2014 this is the baseline\n- Measure performance before and after every change\n- Create indexes with `CONCURRENTLY` (PostgreSQL) to avoid table locks\n- Test in non-production; roll back if write performance or replication lag worsens\n- Document all optimization decisions with before\/after metrics\n- Run `ANALYZE` after bulk data changes to refresh statistics\n\n### MUST NOT DO\n- Apply optimizations without a measured baseline\n- Create redundant or unused indexes\n- Make multiple changes simultaneously (impossible to attribute impact)\n- Ignore write amplification caused by new indexes\n- Neglect `VACUUM` \/ statistics maintenance\n\n## Output Templates\n\nWhen optimizing database performance, provide:\n1. Performance analysis with baseline metrics (query time, cost, buffer hit ratio)\n2. Identified bottlenecks and root causes (with EXPLAIN evidence)\n3. Optimization strategy with specific changes\n4. Implementation SQL \/ config changes\n5. Validation queries to measure improvement\n6. Monitoring recommendations\n\nMaintained by [@jeffallan](https:\/\/github.com\/jeffallan), Principal Consultant at [Synergetic Solutions](https:\/\/synergetic.solutions)\n\n[Documentation](https:\/\/jeffallan.github.io\/claude-skills\/skills\/infrastructure\/database-optimizer\/)\n","use_cases":["Analyzing slow queries and execution plans","Designing optimal index strategies","Tuning database configuration parameters","Optimizing schema design and partitioning","Reducing lock contention and deadlocks","Improving cache hit rates and memory usage"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/Jeffallan\/claude-skills\/blob\/a8be463010e2ca23afe00ea00773aea7128e8971\/plugin\/skills\/database-optimizer\/SKILL.md","source_commit":"a8be463010e2ca23afe00ea00773aea7128e8971","license":null,"version":null,"listing_approved_at":"2026-11-29","created_at":"2026-11-28T18:46:50.000000Z","updated_at":"2026-11-29T10:53:15.000000Z","seo_title":"Database Query Optimization Tool for PostgreSQL and MySQL","seo_description":"Optimize database performance with detailed analysis and code for index design, query rewrites, and configuration tuning. Works with PostgreSQL and MySQL systems.","seo_keywords":["database optimization","slow query","query performance","database tuning","index optimization","execution plan","EXPLAIN ANALYZE","PostgreSQL optimization"]}}