Skip to content
Agentmart
Submit a tool

db-optimize

Identify slow queries and suggest schema, index, or query optimizations.

Skill by ernesto2108

IndexedMIT
Write a review

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/ernesto2108-db-optimize

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

Identify slow queries and suggest schema, index, or query optimizations. Use when user says "slow query", "optimize SQL", "add index", "query performance", "EXPLAIN", or investigating database bottlenecks.

Use cases: backend

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/db-optimize

What's inside

Skill name
db-optimize
Description
Identify slow queries and suggest schema, index, or query optimizations. Use when user says "slow query", "optimize SQL", "add index", "query performance", "EXPLAIN", or investigating database bottlenecks.
License
MIT
Instructions
121 lines in SKILL.md · view on GitHub

Files (1)

  • SKILL.md

SKILL.md (first lines)

# Database Optimization

> Analyze query performance and suggest schema, index, or query improvements.

## Prerequisite

Run `/db-schema-scan` first (or receive schema context inline from orchestrator) to understand current tables, indexes, and relationships.

## When to Use

- User reports slow queries or timeouts
- After adding a new query to a repository
- During QA review of data-heavy features
- Proactive audit of query patterns

## Workflow

### Step 1 — Identify Target Queries

Find queries to analyze:
- Read repository files (`queries/`, `*_psql.go`, `*_repository.go`)
- Look for: full table scans, missing WHERE clauses, JOINs without indexes, N+1 patterns
- If user pointed to a specific query, start there

### Step 2 — Analyze Each Query

For each query, evaluate:

| Check | What to Look For | Fix |
|-------|-----------------|-----|
| **Missing index on WHERE/JOIN** | Column in WHERE or JOIN ON has no index | Create index |
| **Full table scan** | SELECT without WHERE on large table | Add filtering or pagination |
| **N+1 queries** | Loop calling single-row query N times | Batch query with IN clause or JOIN |
| **SELECT *** | Fetching all columns when only 2-3 needed | List specific columns |
| **Missing LIMIT** | List queries without pagination | Add LIMIT + OFFSET or cursor |
| **Unindexed ORDER BY** | Sorting on non-indexed column | Add index or compound index |
| **Implicit type cast** | WHERE varchar_col = 123 (no quotes) | Fix type to avoid cast |
| **OR in WHERE** | Multiple OR conditions prevent index use | UNION or restructure |
| **COUNT(*)** on large table | Full scan for count | Approximate count or cached counter |
| **Tenant isolation** | Multi-tenant query without tenant_id filter | Add tenant_id to WHERE + compound index |

### Step 3 — Index Recommendations

When suggesting indexes:

```markdown
## Index Recommendations

| Table | Suggested Index | Columns | Type | Rationale |
|-------|----------------|---------|------|-----------|
| instances | idx_instances_tenant_status | (tenant_id, status) | btree | Query filters by tenant + status, currently full scan |
| events | idx_events_instance_created | (instance_id, created_at DESC) | btree | Timeline query sorts by date per instance |

### Index Trade-offs
- Each index slows INSERT/UPDATE by ~5-10%
- Only add indexes for queries that run frequently
- Compound indexes: put high-cardinality column first (unless tenant_id for isolation)
- Partial indexes for filtered subsets: `WHERE deleted_at IS NULL`
```

### Step 4 — Query Rewrite Suggestions

If the query itself can be improved:

```markdown
## Query Rewrites

### Before (N+1 pattern)
for each workflow_id:
  SELECT * FROM instances WHERE workflow_id = $1

### After (batch)
SELECT * FROM instances WHERE workflow_id = ANY($1::uuid[])

### Impact: N queries → 1 query
```

## PostgreSQL-Specific Optimizations

| Technique | When | How |
|-----------|------|-----|
| `EXPLAIN ANALYZE` | Verify index usage | Prefix query with EXPLAIN ANALYZE |
| `CREATE INDEX CONCURRENTLY` | Large tables in production | Avoids table lock |
| Partial index | Column has many NULLs you filter out | `CREATE INDEX ... WHERE deleted_at IS NULL` |
| Covering index | Avoid heap lookup | `INCLUDE (col)` in index |
| `GROUPING SETS` | Multiple aggregations in one pass | Replace multiple GROUP BY queries |
| Connection pool tuning | Pool exhaustion errors | `max_open_conns`, `max_idle_conns`, `conn_max_lifetime` |

## Output

```markdown
## Query Performance Report — <project>

### Queries Analyzed: <N>
### Issues Found: <N>

### Critical (fix now)
1. **[file:line]** — <description> → <fix>

### Recommended (improve performance)
1. **[file:line]** — <description> → <fix>

### Index Changes
| Action | SQL |
|--------|-----|
| Add | `CREATE INDEX CONCURRENTLY idx_x ON table (col1, col2)` |
| Drop (unused) | `DROP INDEX idx_y` |

### Estimated Impact
- Query X: ~500ms → ~5ms (index on WHERE columns)
- Query Y: N+1 eliminated, ~N*10ms → ~15ms
```

## Rules

- **Read-only** — suggest changes, don't execute them. DBA agent creates migrations
- **Measure before optimizing** — don't add indexes speculatively. Identify the actual slow query first
- **Compound indexes > multiple single indexes** — one (tenant_id, status, created_at) beats three separate indexes
- **Don't over-index** — every index costs write performance. Only index what's queried frequently

From ernesto2108/forge, MIT.

More from ernesto2108/forge

Signals

Stars
0
Forks
0
Open issues
0
Last commit (this folder)
Apr 3, 2026 (6 months ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from ernesto2108

Cite this page

db-optimize skill. Agent Mart. https://agentmart.dev/skills/ernesto2108-db-optimize (last updated 2026-10-24)