sql-optimization
/sql-optimization
Diagnose and fix slow SQL with the query plan as evidence, not folklore. Use when a query is slow, a table scan appears, or database load climbs.
- Author
- Amey-Thakur ↗
- Repository
- AI-SKILLS ↗
- Licence
- MIT
- Last repository change
- 05.09.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 05.09.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Diagnose and fix slow SQL with the query plan as evidence, not folklore. Use when a query is slow, a table scan appears, or database load climbs.
Original author description
Diagnose and fix slow SQL with the query plan as evidence, not folklore. Use when a query is slow, a table scan appears, or database load climbs.
How it works
- Method
- Rules
SKILL.md preview
---
name: sql-optimization
description: Diagnose and fix slow SQL with the query plan as evidence, not folklore. Use when a query is slow, a table scan appears, or database load climbs.
---
# SQL optimization
Optimize from the plan, not from vibes. Every database ships an
`EXPLAIN`; folklore fixes applied without it make some queries faster and
others quietly worse.
## Method
1. **Measure the actual query** with real parameters and production-like
data volume. A query fast on 1k dev rows tells you nothing about 10M.
Capture: rows examined vs rows returned, time, and the plan
(`EXPLAIN ANALYZE` where available).
2. **Read the plan for the expensive truth.** The usual suspects, in the
order they pay off:
- *Full scan where a seek belongs*: filter or join column lacks a
usable index, or the predicate defeats it (function on the column,
leading wildcard, implicit type cast).
- *Examined ≫ returned*: thousands read to return ten: missing
composite index, or filtering happens after the join instead of in it.
- *Sort or hash spilling*: `ORDER BY`/`GROUP BY`/`DISTINCT` on an
unindexed expression over a large set.
- *N+1 at the application seam*: one query per row in a loop; the plan
looks fine, the trace shows 400 of them.
3. **Fix in this order, cheapest first:**
- Rewrite the predicate to be index-friendly (move the function to the
constant side; match types exactly).
- Add or extend a composite index: equality columns first, then the
range column, then covering columns if the engine supports them. One
good composite beats three single-column indexes.
- Restructure the query: select only needed columns, filter before
joining, replace correlated subqueries with joins or window functions,
paginate by keyset (`WHERE id > ?`) not `OFFSET` at depth.
- Only then reach for denormalization, materialized views, or caching:
real costs that need the earlier steps ruled out.
4. **Verify against the same measurement,** same data, same parameters.
Then check the write side: every index taxes every insert and update on
that table. An index that saves one report and slows every checkout is
a bad trade.
## Rules
- Never claim a fix without before/after numbers from comparable data.
- Distrust `SELECT *` on principle: it defeats covering indexes and widens
every row on the wire.
- A query that cannot be made fast may be the wrong question: say so and
propose the schema or access-pattern change honestly.
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · Amey-Thakur/AI-SKILLS | 100 | MIT |
| nguyengiavu/ngvu-factory | 100 | MIT |
| reddygopalkrishna/developer-agent-skills | 100 | MIT |
| stefan-jansen/claude-code-toolkit | 95 | MIT |
| andreibesleaga/gabbe | 90 | CC-BY-4.0 |
Database snapshot: 13.11.2026 · package version 80a9ddd376cd (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100query-optimization
Diagnose and optimize existing slow SQL queries using execution plans, indexing strategies, query rewriting, and ORM tuning. Use when the user provides a query, performance symptom, or EXPLAIN plan; use sql-query-generation when creating a new query from requirements. - 100sql-optimization-patterns
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance. - 100flux-query
Optimize slow database queries — analyze execution plans, add indexes, rewrite queries. Use when asked about "slow query", "optimize SQL", "query performance", or "explain this query". - 100query-optimizer
Optimize SQL queries for performance with indexing strategies, query rewriting, and execution plan analysis. Use when queries are slow, optimizing database performance, or analyzing query execution. - 100sql-optimizer
Analyze and optimize SQL queries for performance. Use when a user asks to optimize a query, speed up a slow query, analyze a query plan, add indexes, fix N+1 queries, reduce query time, tune database performance, or rewrite SQL for efficiency. Supports PostgreSQL, MySQL, and SQLite. - 95sql-optimization-patterns
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance.