sql-optimizer skill
Explains slow SQL queries in plain English, reads EXPLAIN plans, and suggests concrete rewrites and indexes for PostgreSQL, MySQL, and SQLite, including ORM-generated N+1 problems. Use when the user shares a slow query, an EXPLAIN output, asks why a query is slow, or wants index recommendations.
- GitHub stars
- 5
- Files
- 1
- Last commit
- Nov 4, 2026
- License
- MIT
- Allowed tools
- —
What does the sql-optimizer skill do?
sql-optimizer explains slow SQL queries in plain English, reads EXPLAIN plans, and suggests concrete rewrites and indexes for PostgreSQL, MySQL, and SQLite, including ORM-generated N+1 problems. Use when the user shares a slow query, an EXPLAIN output, asks why a query is slow, or wants index recommendations.
How to install the sql-optimizer skill
Each command copies the skill folder into the client's skills directory. Restart the client or start a new session to load it.Install the sql-optimizer skill in Plugin
/plugin marketplace add milad-ahmd/agent-skills /plugin install all-skills@milad-agent-skills
Install the sql-optimizer skill in Claude Code
~/.claude/skills/sql-optimizer/SKILL.md
$ npx degit milad-ahmd/agent-skills/skills/sql-optimizer ~/.claude/skills/sql-optimizerInstall the sql-optimizer skill in Claude Code (project)
.claude/skills/sql-optimizer/SKILL.md
$ npx degit milad-ahmd/agent-skills/skills/sql-optimizer .claude/skills/sql-optimizerInstall the sql-optimizer skill in Codex
~/.agents/skills/sql-optimizer/SKILL.md
$ npx degit milad-ahmd/agent-skills/skills/sql-optimizer ~/.agents/skills/sql-optimizerInstall the sql-optimizer skill in Gemini CLI
~/.gemini/skills/sql-optimizer/SKILL.md
$ npx degit milad-ahmd/agent-skills/skills/sql-optimizer ~/.gemini/skills/sql-optimizerInstall the sql-optimizer skill in Claude.ai
npx degit milad-ahmd/agent-skills/skills/sql-optimizer sql-optimizer && zip -r sql-optimizer.zip sql-optimizer # claude.ai → Customize → Skills → + → Create skill → Upload a skill → sql-optimizer.zip
SKILL.md
View on GitHubSQL Optimizer
Turn "this query takes 8 seconds" into a measured fix.
1. Understand
- Identify the engine and version, then explain in plain English what the query returns.
- Gather the schema: the tables, the indexes (
\d tablein Postgres,SHOW INDEX FROM tin MySQL) and rough row counts. - Get the real plan. Ask before running anything against production:
- Postgres:
EXPLAIN (ANALYZE, BUFFERS) <query>. Note that ANALYZE executes the query, so wrap writes inBEGIN; ... ROLLBACK;. - MySQL:
EXPLAIN ANALYZE <query>(8.0.18+) orEXPLAIN FORMAT=JSON. - SQLite:
EXPLAIN QUERY PLAN <query>.
- Postgres:
2. Diagnose. Common culprits:
- Seq scan or full table scan on a large table with a selective filter → missing index.
- Functions or casts on indexed columns (
WHERE lower(email) = ...,WHERE created_at::date = ...) → not sargable. - Leading wildcard
LIKE '%x',ORacross different columns, orNOT INwith NULLs. - Large estimate-vs-actual row mismatches → stale statistics (
ANALYZE). SELECT *on wide tables, unnecessaryDISTINCT, orORDER BYwith no supporting index plusLIMIT.- Deep
OFFSETpagination → use keyset pagination. - Correlated subqueries → use JOIN or
EXISTS; also look for an N+1 pattern in ORM code (loops that issue queries).
3. Recommend
For each fix give: the change, why it helps, the expected plan change and the trade-offs.
-- Composite index: equality columns first, then range/sort columns
CREATE INDEX CONCURRENTLY idx_orders_user_created ON orders (user_id, created_at DESC);
- Prefer query rewrites before adding indexes. Every index slows down writes and uses storage.
- Consider partial indexes (
WHERE deleted_at IS NULL), covering indexes (INCLUDE) and expression indexes. - For ORMs, show the fix in the ORM's own syntax: eager loading (
include,select_related,preload) and selecting only the needed columns. - Use
CONCURRENTLY(Postgres) or online DDL for production tables, and ship index changes as a migration in the project's migration tool.
4. Verify
Compare the plan and timing before and after the change. Never claim a speedup you didn't measure; label unmeasured numbers as estimates.
Files in this skill
1sql-optimizer skill questions
How do I install the sql-optimizer skill in Claude Code?
Run npx degit milad-ahmd/agent-skills/skills/sql-optimizer ~/.claude/skills/sql-optimizer. Claude Code loads skills from ~/.claude/skills (all projects) or .claude/skills (one project) and uses a skill when its description matches the task.
Which clients can use the sql-optimizer skill?
Any client that reads the Agent Skills format: Claude Code, Codex, Gemini CLI and claude.ai (upload as a ZIP). Scripts in the skill run only where the client allows code execution.