BeClaude

database-optimizer

New
9.9kCommunityGeneralby jeffallan · MIT

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.

Python869 forks37 issuesUpdated 6/16/2026First seen 5/22/2026

Summary

This skill helps you diagnose and resolve database performance issues by analyzing slow queries, execution plans, and system metrics.

  • It provides expert guidance on index design, query rewrites, configuration tuning, and schema optimization for PostgreSQL and MySQL, enabling you to improve response times and scalability.

Overview

Database Optimizer

Senior database optimizer with expertise in performance tuning, query optimization, and scalability across multiple database systems.

When to Use This Skill

  • 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

Core Workflow

  1. Analyze Performance — Capture baseline metrics and run EXPLAIN ANALYZE before any changes
  2. Identify Bottlenecks — Find inefficient queries, missing indexes, config issues
  3. Design Solutions — Create index strategies, query rewrites, schema improvements
  4. Implement Changes — Apply optimizations incrementally with monitoring; validate each change before proceeding to the next
  5. Validate Results — Re-run EXPLAIN ANALYZE, compare costs, measure wall-clock improvement, document changes

⚠️ Always test changes in non-production first. Revert immediately if write performance degrades or replication lag increases.

Reference Guide

Load detailed guidance based on context:

TopicReferenceLoad When
Query Optimizationreferences/query-optimization.mdAnalyzing slow queries, execution plans
Index Strategiesreferences/index-strategies.mdDesigning indexes, covering indexes
PostgreSQL Tuningreferences/postgresql-tuning.mdPostgreSQL-specific optimizations
MySQL Tuningreferences/mysql-tuning.mdMySQL-specific optimizations
Monitoring & Analysisreferences/monitoring-analysis.mdPerformance metrics, diagnostics

Common Operations & Examples

Identify Top Slow Queries (PostgreSQL)

sql
-- Requires pg_stat_statements extension
SELECT query,
       calls,
       round(total_exec_time::numeric, 2)  AS total_ms,
       round(mean_exec_time::numeric, 2)   AS mean_ms,
       round(stddev_exec_time::numeric, 2) AS stddev_ms,
       rows
FROM   pg_stat_statements
ORDER  BY mean_exec_time DESC
LIMIT  20;

Capture an Execution Plan

sql
-- Use BUFFERS to expose cache hit vs. disk read 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';

Reading EXPLAIN Output — Key Patterns to Find

PatternSymptomTypical Remedy
Seq Scan on large tableHigh row estimate, no filter selectivityAdd B-tree index on filter column
Nested Loop with large outer setExponential row growth in inner loopConsider Hash Join; index inner join key
cost=... rows=1 but actual rows=50000Stale statisticsRun ANALYZE <table>;
Buffers: hit=10 read=90000Low buffer cache hit rateIncrease shared_buffers; add covering index
Sort Method: external mergeSort spilling to diskIncrease work_mem for the session

Create a Covering Index

sql
-- Covers the filter AND the projected columns, eliminating a heap fetch
CREATE INDEX CONCURRENTLY idx_orders_status_created_covering
    ON orders (status, created_at)
    INCLUDE (customer_id, total_amount);

Validate Improvement

sql
-- Before optimization: save plan & timing
EXPLAIN (ANALYZE, BUFFERS) <query>;   -- note "Execution Time: X ms"

-- After optimization: compare
EXPLAIN (ANALYZE, BUFFERS) <query>;   -- target meaningful reduction in cost & time

-- Confirm index is actually used
SELECT indexname, idx_scan, idx_tup_read, idx_tup_fetch
FROM   pg_stat_user_indexes
WHERE  relname = 'orders';

MySQL: Find Slow Queries

sql
-- Inspect slow query log candidates
SELECT * FROM performance_schema.events_statements_summary_by_digest
ORDER  BY SUM_TIMER_WAIT DESC
LIMIT  20;

-- Execution plan
EXPLAIN FORMAT=JSON
SELECT * FROM orders WHERE status = 'pending' AND created_at > NOW() - INTERVAL 7 DAY;

Constraints

MUST DO

  • Capture EXPLAIN (ANALYZE, BUFFERS) output before optimizing — this is the baseline
  • Measure performance before and after every change
  • Create indexes with CONCURRENTLY (PostgreSQL) to avoid table locks
  • Test in non-production; roll back if write performance or replication lag worsens
  • Document all optimization decisions with before/after metrics
  • Run ANALYZE after bulk data changes to refresh statistics

MUST NOT DO

  • Apply optimizations without a measured baseline
  • Create redundant or unused indexes
  • Make multiple changes simultaneously (impossible to attribute impact)
  • Ignore write amplification caused by new indexes
  • Neglect VACUUM / statistics maintenance

Output Templates

When optimizing database performance, provide:

  1. Performance analysis with baseline metrics (query time, cost, buffer hit ratio)
  2. Identified bottlenecks and root causes (with EXPLAIN evidence)
  3. Optimization strategy with specific changes
  4. Implementation SQL / config changes
  5. Validation queries to measure improvement
  6. Monitoring recommendations

Documentation

Install & Usage

1
Create the skills directory
mkdir -p .claude/skills
2
Download the skill file
mkdir -p .claude/skills && curl -o .claude/skills/database-optimizer.md https://raw.githubusercontent.com/jeffallan/claude-skills/main/skills/database-optimizer/SKILL.md
3
Invoke in Claude Code
/database-optimizer

Use Cases

Analyze a slow-running query using EXPLAIN ANALYZE and suggest optimizations.
Design a covering index to eliminate table scans for a frequent SELECT query.
Tune PostgreSQL configuration parameters to improve cache hit rates.
Resolve lock contention causing frequent deadlocks in a high-concurrency workload.
Partition a large table to speed up range queries and maintenance operations.
Rewrite a suboptimal JOIN query to reduce execution time by 50%.

Usage Examples

1

/database-optimizer Analyze the execution plan for this query: SELECT * FROM orders WHERE customer_id = 123 ORDER BY created_at DESC;

2

I have a slow query that takes 10 seconds. Can you help me optimize it? Here's the EXPLAIN ANALYZE output: ...

3

/database-optimizer Suggest indexes for a table with columns user_id, status, and created_at, queried by user_id and status.

View source on GitHub
designai-agentsclaudeclaude-codeclaude-marketplaceclaude-skills

Security Audits

LicensePassSourceWarnRepositoryPass

Frequently Asked Questions

What is database-optimizer?

This skill helps you diagnose and resolve database performance issues by analyzing slow queries, execution plans, and system metrics. It provides expert guidance on index design, query rewrites, configuration tuning, and schema optimization for PostgreSQL and MySQL, enabling you to improve response times and scalability.

How to install database-optimizer?

To install database-optimizer: create the skills directory (mkdir -p .claude/skills), then run: mkdir -p .claude/skills && curl -o .claude/skills/database-optimizer.md https://raw.githubusercontent.com/jeffallan/claude-skills/main/skills/database-optimizer/SKILL.md. Finally, /database-optimizer in Claude Code.

What is database-optimizer best for?

database-optimizer is a skill categorized under General. It is designed for: design. Created by jeffallan.

What can I use database-optimizer for?

database-optimizer is useful for: Analyze a slow-running query using EXPLAIN ANALYZE and suggest optimizations.; Design a covering index to eliminate table scans for a frequent SELECT query.; Tune PostgreSQL configuration parameters to improve cache hit rates.; Resolve lock contention causing frequent deadlocks in a high-concurrency workload.; Partition a large table to speed up range queries and maintenance operations.; Rewrite a suboptimal JOIN query to reduce execution time by 50%..