/sql-optimizer
Analyzes SQL queries for missing indexes, N+1 patterns, suboptimal joins, and full table scans. Interprets EXPLAIN, detects anti-patterns, rewrites queries. Triggers on: "optimize this query", "slow query", "add indexes", "explain plan", "N+1 query", "why is this query slow".
$ npx -y skills add Mathews-Tom/armory --skill sql-optimizer --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/sql-optimizer
Context preview
The summary Claude sees to decide when to auto-load this skill.
Analyzes SQL queries for missing indexes, N+1 patterns, suboptimal joins, and full table scans. Interprets EXPLAIN, detects anti-patterns, rewrites queries. Triggers on: "optimize this query", "slow query", "add indexes", "explain plan", "N+1 query", "why is this query slow".
SKILL.md
sql-optimizer.SKILL.mdname: sql-optimizer
description: 'Analyzes SQL queries for missing indexes, N+1 patterns, suboptimal joins, and full table scans. Interprets EXPLAIN, detects anti-patterns, rewrites queries. Triggers on: "optimize this query", "slow query", "add indexes", "explain plan", "N+1 query", "why is this query slow".'
metadata:
version: 1.1.1
category: data
tags: [sql, performance, database, optimization]
difficulty: intermediate
phase: build
SQL Optimizer
Systematic SQL performance analysis: parse query structure, interpret EXPLAIN plans, detect anti-patterns (N+1, full scans, cartesian joins), recommend indexes, and rewrite queries — with explanations of WHY each change improves performance, not just WHAT changed.
Reference Files
| File | Contents | Load When | | --------------------------------- | ------------------------------------------------------------------------- | ---------------------------------- | | `references/anti-patterns.md` | Common SQL anti-patterns with detection rules and fixes | Always | | `references/index-strategies.md` | Index type selection, composite index ordering, covering indexes | Index recommendations needed | | `references/explain-guide.md` | Reading EXPLAIN output for PostgreSQL, MySQL, SQLite | EXPLAIN plan provided | | `references/join-optimization.md` | Join type selection, join order optimization, subquery-to-join conversion | Query contains joins or subqueries |
Prerequisites
- The SQL query to optimize
- Database engine (PostgreSQL, MySQL, SQLite) — optimization differs by engine
- Table schemas and approximate row counts (helpful but not required)
- EXPLAIN output (highly valuable when available)
Workflow
Phase 1: Query Analysis
Parse the SQL to understand its structure:
1. **Identify operations** — SELECT columns, FROM tables, JOIN conditions, WHERE filters, GROUP BY, ORDER BY, HAVING, subqueries. 2. **Map table relationships** — Which tables are joined? On what keys? Are there implicit cartesian products? 3. **Detect immediate red flags**:
- `SELECT *` — fetching unnecessary columns
- Functions on indexed columns in WHERE — prevents index use
- `OR` in WHERE — often prevents index use
- Correlated subqueries — potential N+1
- Missing WHERE on DELETE/UPDATE — dangerous
Phase 2: EXPLAIN Interpretation
If an EXPLAIN plan is provided:
1. **Scan types** — Sequential Scan (bad for large tables), Index Scan (good), Index Only Scan (best), Bitmap Index Scan (acceptable). 2. **Join methods** — Nested Loop (good for small tables), Hash Join (good for equi-joins), Merge Join (good for sorted data). 3. **Row estimates** — Compare estimated rows with actual rows. Large discrepancies indicate stale statistics (`ANALYZE`). 4. **Cost hotspots** — Highest-cost node is the bottleneck. Optimize there first. 5. **Sort operations** — External sorts (disk) are expensive. Consider indexes that match ORDER BY.
Phase 3: Anti-Pattern Detection
Check for known performance anti-patterns (see `references/anti-patterns.md`):
| Pattern | Detection | Impact | | --------------------------- | ------------------------------ | -------------------------- | | SELECT \* | Star in select list | Transfers unnecessary data | | N+1 queries | Loop with query inside | N additional roundtrips | | Function on indexed column | `WHERE UPPER(name) = 'X'` | Index bypass | | Implicit type cast | String compared to integer | Index bypass | | Missing join condition | Cartesian product | Exponential rows | | LIKE '%prefix' | Leading wildcard | Full scan | | OR with different columns | `WHERE a=1 OR b=2` | Index bypass | | SELECT DISTINCT as band-aid | Hides duplicate-producing join | Fix the join instead |
Phase 4: Optimization
1. **Index recommendations** — Based on WHERE, JOIN, ORDER BY, GROUP BY columns. Consider composite indexes for multi-column conditions. 2. **Query rewrite** — Convert correlated subqueries to JOINs, replace `IN (SELECT...)` with EXISTS, use CTEs for readability without performance cost (PostgreSQL 12+ may inline CTEs). 3. **Schema suggestions** — Denormalization, materialized views, partitioning (mention only when query-level optimization is insufficient).
Phase 5: Output
Present the original query, detected issues, recommended indexes, rewritten query, and explanation of each change.
Output Format
## SQL Optimization Analysis
### Original Query
```sql
{original SQL}Issues Detected
| # | Issue | Severity | Location | Impact | | --- | ------- | ----------------- | ------------------- | ---------------- | | 1 | {issue} | {High/Medium/Low} | {WHERE/JOIN/SELECT} | {what it causes} |
EXPLAIN Interpretation
{If EXPLAIN provided}
- **Bottleneck:** {node type} on `{table}` (cost: {N})
- **Rows scanned:** {N} (estimated {M})
- **Index used:** {name or "None"}
- **Key insight:** {what this reveals}
Recommended Indexes
-- {Reason for this index}
CREATE INDEX {name} ON {table}({columns});Optimized Query
{rewritten query}Change Explanation
1. **{Change}** — {Why this improves performance. Include estimated impact.}
Expected Improvement
- Scan type: {before} → {after}
- Estimated rows scanned: {before} → {after}
- Index usage: {before} → {after}
## Configuring Scope
| Mode | Input | Depth | When to Use
Read more
name: sql-optimizer description: 'Analyzes SQL queries for missing indexes, N+1 patterns, suboptimal joins, and full table scans. Interprets EXPLAIN, detects anti-patterns, rewrites queries. Triggers on: "optimize this query", "slow query", "add indexes", "explain plan", "N+1 query", "why is this query slow".' metadata: version: 1.1.1 category: data tags: [sql, performance, database, optimization] difficulty: intermediate phase: build
SQL Optimizer
Systematic SQL performance analysis: parse query structure, interpret EXPLAIN plans, detect anti-patterns (N+1, full scans, cartesian joins), recommend indexes, and rewrite queries — with explanations of WHY each change improves performance, not just WHAT changed.
Reference Files
| File | Contents | Load When | | --------------------------------- | ------------------------------------------------------------------------- | ---------------------------------- | | `references/anti-patterns.md` | Common SQL anti-patterns with detection rules and fixes | Always | | `references/index-strategies.md` | Index type selection, composite index ordering, covering indexes | Index recommendations needed | | `references/explain-guide.md` | Reading EXPLAIN output for PostgreSQL, MySQL, SQLite | EXPLAIN plan provided | | `references/join-optimization.md` | Join type selection, join order optimization, subquery-to-join conversion | Query contains joins or subqueries |
Prerequisites
- The SQL query to optimize
- Database engine (PostgreSQL, MySQL, SQLite) — optimization differs by engine
- Table schemas and approximate row counts (helpful but not required)
- EXPLAIN output (highly valuable when available)
Workflow
Phase 1: Query Analysis
Parse the SQL to understand its structure:
1. **Identify operations** — SELECT columns, FROM tables, JOIN conditions, WHERE filters, GROUP BY, ORDER BY, HAVING, subqueries. 2. **Map table relationships** — Which tables are joined? On what keys? Are there implicit cartesian products? 3. **Detect immediate red flags**:
- `SELECT *` — fetching unnecessary columns
- Functions on indexed columns in WHERE — prevents index use
- `OR` in WHERE — often prevents index use
- Correlated subqueries — potential N+1
- Missing WHERE on DELETE/UPDATE — dangerous
Phase 2: EXPLAIN Interpretation
If an EXPLAIN plan is provided:
1. **Scan types** — Sequential Scan (bad for large tables), Index Scan (good), Index Only Scan (best), Bitmap Index Scan (acceptable). 2. **Join methods** — Nested Loop (good for small tables), Hash Join (good for equi-joins), Merge Join (good for sorted data). 3. **Row estimates** — Compare estimated rows with actual rows. Large discrepancies indicate stale statistics (`ANALYZE`). 4. **Cost hotspots** — Highest-cost node is the bottleneck. Optimize there first. 5. **Sort operations** — External sorts (disk) are expensive. Consider indexes that match ORDER BY.
Phase 3: Anti-Pattern Detection
Check for known performance anti-patterns (see `references/anti-patterns.md`):
| Pattern | Detection | Impact | | --------------------------- | ------------------------------ | -------------------------- | | SELECT \* | Star in select list | Transfers unnecessary data | | N+1 queries | Loop with query inside | N additional roundtrips | | Function on indexed column | `WHERE UPPER(name) = 'X'` | Index bypass | | Implicit type cast | String compared to integer | Index bypass | | Missing join condition | Cartesian product | Exponential rows | | LIKE '%prefix' | Leading wildcard | Full scan | | OR with different columns | `WHERE a=1 OR b=2` | Index bypass | | SELECT DISTINCT as band-aid | Hides duplicate-producing join | Fix the join instead |
Phase 4: Optimization
1. **Index recommendations** — Based on WHERE, JOIN, ORDER BY, GROUP BY columns. Consider composite indexes for multi-column conditions. 2. **Query rewrite** — Convert correlated subqueries to JOINs, replace `IN (SELECT...)` with EXISTS, use CTEs for readability without performance cost (PostgreSQL 12+ may inline CTEs). 3. **Schema suggestions** — Denormalization, materialized views, partitioning (mention only when query-level optimization is insufficient).
Phase 5: Output
Present the original query, detected issues, recommended indexes, rewritten query, and explanation of each change.
Output Format
## SQL Optimization Analysis
### Original Query
```sql
{original SQL}Issues Detected
| # | Issue | Severity | Location | Impact | | --- | ------- | ----------------- | ------------------- | ---------------- | | 1 | {issue} | {High/Medium/Low} | {WHERE/JOIN/SELECT} | {what it causes} |
EXPLAIN Interpretation
{If EXPLAIN provided}
- **Bottleneck:** {node type} on `{table}` (cost: {N})
- **Rows scanned:** {N} (estimated {M})
- **Index used:** {name or "None"}
- **Key insight:** {what this reveals}
Recommended Indexes
-- {Reason for this index}
CREATE INDEX {name} ON {table}({columns});Optimized Query
{rewritten query}Change Explanation
1. **{Change}** — {Why this improves performance. Include estimated impact.}
Expected Improvement
- Scan type: {before} → {after}
- Estimated rows scanned: {before} → {after}
- Index usage: {before} → {after}
## Configuring Scope | Mode | Input | Depth | When to Use
Curated, production-grade skills, agents, hooks, rules, commands, utilities, and presets for AI coding agents. No magic, no demos — battle-tested workflows built for developers who use AI seriously.
Repo: Mathews-Tom/armory
Other skills on armory.
- /adr-writer
Generates Architecture Decision Records capturing context, rationale, alternatives, and consequences in numbered status-tracked format. Triggers on: "write an ADR", "document this decision", "architecture decision record", "decision record", "design decision", "ADR for".
Open skill - /agent-builder
Build AI agents and automate Claude Code programmatically via the Claude Agent SDK and headless CLI mode. Covers Python SDK, claude -p, SDK MCP servers, hooks, sessions. Triggers on: "build an agent", "agent SDK", "headless mode", "automate Claude", "programmatic agent".
Open skill - /api-docs-generator
Audits and enhances FastAPI and REST API documentation: missing descriptions, response codes, examples, docstrings, Pydantic models, OpenAPI spec. Triggers on: "generate API docs", "document this API", "OpenAPI for", "FastAPI docs", "document endpoints", "swagger docs".
Open skill - /architecture-diagram
Generate layered architecture diagrams as self-contained HTML with inline SVG icons, CSS Grid containers, and connection overlays. Triggers on: "architecture diagram", "infra diagram", "system diagram", "deployment diagram", "topology", "draw architecture". NOT for architecture
Open skill - /architecture-reviewer
Architecture reviews across 7 dimensions (structural, scalability, enterprise readiness, performance, security, ops, data) with scored reports. Triggers on: "review architecture", "critique design", "audit system", "assess scalability", "enterprise readiness", "technical due
Open skill - /arxiv-figures
Optimize and prepare figures for arXiv submission: format conversion (EPS/PDF/PNG/JPG), size reduction, metadata stripping, processor compatibility (DVI vs PDFLaTeX). Triggers on: "optimize figures for arXiv", "reduce figure size", "convert figures for arXiv", "fix arXiv
Open skill

