home / skills / 404kidwiz / claude-supercode-skills / database-optimizer-skill

database-optimizer-skill skill

/database-optimizer-skill

This skill helps you optimize database performance across PostgreSQL, MySQL, MongoDB, and Redis by refining queries, indexes, and configurations for sub-second

npx playbooks add skill 404kidwiz/claude-supercode-skills --skill database-optimizer-skill

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
6.3 KB
---
name: database-optimizer
description: Use when user needs database query optimization, performance tuning, index strategies, execution plan analysis, or scalability across PostgreSQL, MySQL, MongoDB, Redis, and other database systems.
---

# Database Optimizer

## Purpose

Provides expert database performance tuning and optimization across major database systems (PostgreSQL, MySQL, MongoDB, Redis) specializing in query optimization, index design, execution plan analysis, and system configuration. Achieves sub-second query performance and optimal resource utilization through systematic optimization approaches.

## When to Use

- Query execution time exceeds performance targets (>100ms for OLTP, >5s for analytics)
- Database CPU/memory/I/O utilization consistently above 70%
- Application experiencing database connection exhaustion or timeouts
- Slow query log shows problematic patterns or missing indexes
- Database struggling to handle expected load or traffic spikes
- Replication lag exceeding acceptable thresholds (>1s for critical systems)
- Need to optimize database configuration for specific workload (OLTP vs OLAP)
- Planning database capacity or horizontal scaling strategy

## Quick Start

**Invoke this skill when:**
- Slow queries need optimization (EXPLAIN ANALYZE shows issues)
- Index strategy needs design or review
- Database configuration tuning required
- Capacity planning or scaling decisions needed

**Do NOT invoke when:**
- Simple CRUD operations with no performance issues
- Schema design without optimization focus (use database-administrator)
- Application-level caching only (use backend-developer)

## Core Capabilities

### Query Optimization
- Analyzing execution plans and identifying bottlenecks
- Rewriting queries for optimal performance
- Optimizing joins, subqueries, and aggregations
- Implementing query result caching strategies

### Index Design
- Designing appropriate index types (B-tree, GIN, BRIN, hash)
- Creating composite indexes for multi-column queries
- Implementing partial indexes for specific query patterns
- Managing index maintenance and avoiding bloat

### Database Configuration
- Tuning database parameters for specific workloads
- Optimizing memory allocation (buffer pool, cache sizes)
- Configuring connection pooling and concurrency settings
- Implementing partitioning strategies for large tables

### Performance Monitoring
- Setting up query performance monitoring and alerting
- Analyzing slow query logs and identifying patterns
- Implementing database metrics collection (EXPLAIN ANALYZE)
- Creating performance baselines and capacity planning

## Decision Framework

### Optimization Priority Matrix

| Symptom | First Action | Tool |
|---------|--------------|------|
| Query >100ms | EXPLAIN ANALYZE | Execution plan review |
| High CPU | pg_stat_statements | Find top queries |
| High I/O | Index review | Missing index detection |
| Connection exhaustion | Pool tuning | PgBouncer/connection limits |
| Replication lag | Write optimization | Batch operations |

### Index Decision Tree

```
Query Performance Issue
│
├─ WHERE clause filtering?
│  └─ Create B-tree index on filter columns
│
├─ JOIN operations slow?
│  └─ Index foreign key columns
│
├─ ORDER BY/GROUP BY expensive?
│  └─ Include sort columns in index
│
├─ Covering index possible?
│  └─ Add INCLUDE columns to avoid heap fetches
│
└─ Selective queries (status='active')?
   └─ Use partial index with WHERE clause
```

## Core Workflow: Slow Query Optimization

**Scenario**: Production query taking 3.2s, needs to be <100ms

**Step 1: Capture baseline with EXPLAIN ANALYZE**

```sql
EXPLAIN (ANALYZE, BUFFERS, VERBOSE) 
SELECT u.id, u.email, 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.email
ORDER BY total_spent DESC
LIMIT 100;
```

**Step 2: Identify issues from execution plan**
- Sequential scans instead of index scans
- High shared reads (cache misses)
- Missing indexes on filter/join columns

**Step 3: Create strategic indexes**

```sql
-- Covering index for users with partial index
CREATE INDEX CONCURRENTLY idx_users_status_created_active 
  ON users (status, created_at) 
  INCLUDE (id, email)
  WHERE status = 'active';

-- Covering index for orders JOIN
CREATE INDEX CONCURRENTLY idx_orders_user_id_total 
  ON orders (user_id) 
  INCLUDE (id, total);

-- Update statistics
ANALYZE users;
ANALYZE orders;
```

**Step 4: Verify optimization**

```sql
EXPLAIN (ANALYZE, BUFFERS, VERBOSE) 
-- Same query - should now show:
-- - Index Only Scan instead of Seq Scan
-- - Heap Fetches: 0
-- - Execution Time: <100ms
```

**Expected outcome**:
- Execution time reduced by 95%+ (3205ms -> 87ms)
- Buffer reads eliminated (all hits from cache)
- Sequential scans replaced with index scans
- Query plan stable and predictable

## Quick Reference: Performance Targets

| Metric | OLTP Target | Analytics Target |
|--------|-------------|------------------|
| P50 latency | <50ms | <2s |
| P95 latency | <100ms | <5s |
| P99 latency | <200ms | <10s |
| Cache hit ratio | >95% | >90% |
| Index usage | >95% | >80% |

## Quick Reference: Configuration Guidelines

| Parameter | Formula | Example (32GB RAM) |
|-----------|---------|-------------------|
| shared_buffers | 25% of RAM | 8GB |
| effective_cache_size | 75% of RAM | 24GB |
| work_mem | RAM / max_connections / 4 | 40MB |
| maintenance_work_mem | 10% of RAM | 2GB |
| random_page_cost | 1.1 (SSD) / 4.0 (HDD) | 1.1 |

## Red Flags - When to Escalate

| Observation | Action |
|-------------|--------|
| Query complexity explosion | Escalate to architect for schema redesign |
| Replication lag >10s | Escalate to DBA for infrastructure review |
| Connection pool exhaustion | Review application connection handling |
| Disk I/O saturation | Consider read replicas or caching layer |

## Additional Resources

- **Detailed Technical Reference**: See [REFERENCE.md](REFERENCE.md)
  - Database configuration tuning workflows
  - Partitioning strategies for time-series data
  - Advanced monitoring queries
  
- **Code Examples & Patterns**: See [EXAMPLES.md](EXAMPLES.md)
  - Anti-patterns (over-indexing, premature denormalization)
  - Quality checklist for optimization projects
  - Index monitoring and maintenance queries

Overview

This skill provides expert database performance tuning across PostgreSQL, MySQL, MongoDB, Redis and other systems. It focuses on query optimization, index strategy, execution plan analysis, and configuration to achieve sub-second queries and efficient resource use. Use it to diagnose slow queries, reduce CPU/I/O pressure, and scale databases predictably.

How this skill works

I inspect execution plans (EXPLAIN / EXPLAIN ANALYZE), slow query logs, and engine-specific metrics to identify hotspots. I recommend rewrites, index designs (B-tree, GIN, BRIN, partial, covering), and configuration changes (memory, connection pooling, partitioning). I validate improvements with repeatable benchmarks and explain-plan verification.

When to use it

  • When queries exceed latency targets (>100ms OLTP or >5s analytics).
  • If CPU, memory, or disk I/O stay consistently above ~70%.
  • When slow query logs show missing indexes, sequential scans, or high buffer reads.
  • When replication lag, connection exhaustion, or timeouts occur under load.
  • When planning capacity, sharding, or horizontal scaling strategies.

Best practices

  • Capture a baseline with EXPLAIN ANALYZE and slow-query samples before changes.
  • Prefer targeted covering or partial indexes to reduce I/O and avoid bloat.
  • Tune memory and cache settings based on workload (OLTP vs OLAP) and RAM.
  • Use connection pooling (PgBouncer, proxies) to avoid connection exhaustion.
  • Measure after each change; verify plan stability and run representative load tests.

Example use cases

  • Reduce a 3.2s report query to <100ms by adding partial and covering indexes and rewriting aggregations.
  • Identify and fix a high-CPU offender by finding a nested loop join on large tables and converting to a hash join with proper indexes.
  • Tune PostgreSQL shared_buffers, effective_cache_size, and work_mem for a 32GB host to improve cache hit ratio.
  • Design partitioning and maintenance strategy for multi-terabyte time-series data to improve query and vacuum performance.
  • Recommend Redis caching patterns and eviction policies to reduce read load on primary database.

FAQ

Can this skill handle multiple database types?

Yes. It covers relational and NoSQL engines (Postgres, MySQL, MongoDB, Redis) and adapts recommendations to each system's features.

Will you change production settings directly?

I provide concrete, safe change plans and SQL commands (with CONCURRENTLY where applicable) and recommend testing and backups before applying to production.

database-optimizer-skill skill by 404kidwiz/claude-supercode-skills