Install this skill: copy and save to ~/.claude/skills/query-optimizer/SKILL.md

Query Optimizer

Analyzes slow queries, suggests indexes, rewrites inefficient patterns, and explains the optimization with EXPLAIN output.

5 min|Advanced
BuildDeep WorkDeveloper
SKILL.md~/.claude/skills/query-optimizer/
---
name: query-optimizer
description: |
  Analyzes and optimizes slow database queries.
  IMPORTANT: Activate this skill whenever the user says "query is slow",
  "optimize this query", "database performance", or "slow database".
  This skill MUST be used for all query optimization requests.
---

# Query Optimizer

Analyzes slow database queries, identifies the bottleneck, suggests indexes, rewrites inefficient patterns, and explains the optimization. Uses EXPLAIN output to verify improvements.

**Trigger:**

Say any of these to activate this skill:
- "this query is slow"
- "optimize this query"
- "database is slow"
- "Use skill: query optimizer"

**How It Works:**

1. Read the slow query and understand what it's trying to do
2. Check the table schema for existing indexes
3. Identify performance issues: missing indexes, N+1 patterns, full table scans, unnecessary JOINs
4. Run EXPLAIN (or EXPLAIN ANALYZE) on the query to see the execution plan
5. Suggest specific fixes: add indexes, rewrite subqueries as JOINs, add WHERE clauses, use pagination
6. Apply the optimization and re-run EXPLAIN to verify improvement
7. If using an ORM, show both the ORM code change and the resulting SQL

**Guidelines:**

- Always check EXPLAIN output before and after optimization
- Adding indexes speeds up reads but slows down writes; consider the tradeoff
- For N+1 queries, prefer eager loading or batched queries
- Avoid SELECT * in production; select only the columns needed
- Consider query caching for expensive, frequently-run queries

**Works With:**

- Best with: Claude Code CLI (can run queries and EXPLAIN)
- Also works: Cursor, Windsurf (share the query and schema)
- Expects: Access to the database or at minimum the schema and query

**Output Format:**

- Query analysis: what's slow and why
- EXPLAIN output (before)
- Optimization applied: index added, query rewritten, or both
- EXPLAIN output (after) showing the improvement

Analyzes slow database queries, identifies the bottleneck, suggests indexes, rewrites inefficient patterns, and explains the optimization. Uses EXPLAIN output to verify improvements.

Say any of these to activate this skill: - "this query is slow" - "optimize this query" - "database is slow" - "Use skill: query optimizer"

1. Read the slow query and understand what it's trying to do 2. Check the table schema for existing indexes 3. Identify performance issues: missing indexes, N+1 patterns, full table scans, unnecessary JOINs 4. Run EXPLAIN (or EXPLAIN ANALYZE) on the query to see the execution plan 5. Suggest specific fixes: add indexes, rewrite subqueries as JOINs, add WHERE clauses, use pagination 6. Apply the optimization and re-run EXPLAIN to verify improvement 7. If using an ORM, show both the ORM code change and the resulting SQL

  • Always check EXPLAIN output before and after optimization
  • Adding indexes speeds up reads but slows down writes; consider the tradeoff
  • For N+1 queries, prefer eager loading or batched queries
  • Avoid SELECT * in production; select only the columns needed
  • Consider query caching for expensive, frequently-run queries
  • Best with: Claude Code CLI (can run queries and EXPLAIN)
  • Also works: Cursor, Windsurf (share the query and schema)
  • Expects: Access to the database or at minimum the schema and query
  • Query analysis: what's slow and why
  • EXPLAIN output (before)
  • Optimization applied: index added, query rewritten, or both
  • EXPLAIN output (after) showing the improvement
Query Optimizer | Library | Modern Vibe Coding