Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Sql Optimization?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/vectora-os-sql-optimization)
---
name: sql-optimization
description: SQL query optimization — reading execution plans, index design (composite order, covering, partial), N+1 detection, join strategies, pagination (keyset), statistics, avoiding functions on indexed columns, batching, measuring under realistic data. Use for slow queries or query review (SQL optimization, EXPLAIN, slow query, index, N+1, keyset pagination).
metadata:
category: performance
---
# SQL optimization
## Purpose
Make queries fast by evidence, not folklore.
**Use when:** slow queries or reviews of data access code.
**Not for:** schema redesign.
## Inputs
- Query, plan, data distribution.
## Outputs
- Optimized query/index with plans and timings before/after.
## Read first
- [Project Brain](../../../project-brain/README.md) — `npm run brain -- query "sql optimization"` for prior decisions, patterns and anti-patterns.
- [Performance report template](../../../templates/performance-report.md)
## Steps
1. **Measure.** Capture the plan with actual timings on realistic data.
Done when baseline recorded.
2. **Diagnose.** Find the cost: seq scans, misestimates, sorts, nested loops over many rows.
Done when cost location named.
3. **Fix.** Index, rewrite, batch, or keyset paginate.
Done when one change at a time.
4. **Verify.** Same data, same parameters, compare plans and timings.
Done when improvement is evidenced.
## Best practices
- Composite index order: equality, then range/sort.
- Check write cost of new indexes.
## Common mistakes
- Optimizing on tiny dev datasets.
- Adding indexes without checking plans.
## Example
Offset 50,000 page took 1.9 s; keyset on `(created_at,id)` 6 ms.
## Reusable prompt
```text
Apply the SQL optimization skill (.agents/skills/sql-optimization/SKILL.md).
Context: {{task card or question; links to PRD, ADRs, code paths}}
Constraints: {{approved thresholds, deadlines, allowed files}}
Capture the plan, locate the cost, propose a fix and verify with before/after plans on the same data.
Return: Optimized query/index with plans and timings before/after..
Cite evidence (file:line, command output, source) for every claim; list open questions separately.
```