sql-optimization

A guide to improving SQL queries, which are instructions used to read or change data in relational databases such as PostgreSQL, MySQL, and SQLite.

In plain words
What is it for?
Use it to inspect query plans with EXPLAIN, choose indexes, rewrite queries, improve joins, prevent N+1 queries, and investigate high database CPU usage.
Why use it?
It helps find why database queries are slow, such as reading every row, using poor indexes, inefficient joins, or making one query per result in an N+1 problem.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/stefan-jansen/claude-code-toolkit/sql-optimization
Any agent
npx skills add stefan-jansen/claude-code-toolkit --skill sql-optimization
Clone the repo
git clone --depth 1 https://github.com/stefan-jansen/claude-code-toolkit

Made for: Claude Code, Codex.

Per session 78 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 4,573 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00078 $0.04573
Opus 5 $0.00039 $0.02286
Sonnet 5 $0.00016 $0.00915
Haiku 4.5 $0.00008 $0.00457

Measured yesterday against content hash 23861c016b6c, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

sql-optimization scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/general-dev/sql-optimization/SKILL.md · 736 lines

How it starts

The opening of the file, as written. The whole thing — 736 lines — stays where its author put it; the contents beside it link to each section on GitHub.

SQL Query Optimization Patterns

Comprehensive guide to optimizing SQL queries for performance, including EXPLAIN plan analysis, index design strategies, query rewriting patterns, and N+1 query detection. Works across PostgreSQL, MySQL, and SQLite.


Quick Reference

When to use this skill:

  • Slow database queries (>100ms for simple queries, >1s for complex)
  • High database CPU usage
  • Analyzing EXPLAIN plans
  • Designing database indexes
  • Debugging N+1 query problems
  • Optimizing JOIN operations
  • Reducing table scans

Common triggers:

  • "This query is too slow"
  • "How do I optimize this SQL"
  • "What indexes should I add"
  • "Explain this EXPLAIN plan"
  • "Fix N+1 queries"
  • "Database CPU at 100%"

Typical improvements:

  • 3 seconds → 50ms (60x faster)
  • Full table scan → Index scan
  • 1000 queries → 2 queries (N+1 elimination)

Part 1: Understanding EXPLAIN Plans

Reading PostgreSQL EXPLAIN

EXPLAIN ANALYZE
SELECT u.name, p.title
FROM users u
JOIN posts p ON u.id = p.user_id
WHERE u.created_at > '2024-01-01'
ORDER BY p.created_at DESC
LIMIT 10;

Key Metrics to Watch:

  • Seq Scan (bad): Full table scan, reads every row
  • Index Scan (good): Uses index, reads only needed rows
  • Cost: Estimated computational cost (lower is better)
  • Actual time: Real execution time in milliseconds
  • Rows: Number of rows processed at each step

Bad EXPLAIN Example

Seq Scan on users u  (cost=0.00..1234.00 rows=1000 width=50)
                     (actual time=0.123..45.678 rows=950 loops=1)
  Filter: (created_at > '2024-01-01'::date)
  Rows Removed by Filter: 50000
Planning Time: 0.234 ms
Execution Time: 3456.789 ms

Problems:

  • Seq Scan: Reading entire table (50,950 rows)
  • Rows Removed by Filter: Filtering after reading (wasteful)
  • Execution Time: 3.5 seconds (way too slow)

Good EXPLAIN Example (After Index)

Index Scan using users_created_at_idx on users u
  (cost=0.29..123.45 rows=950 width=50)
  (actual time=0.012..3.456 rows=950 loops=1)
  Index Cond: (created_at > '2024-01-01'::date)
Planning Time: 0.123 ms
Execution Time: 4.567 ms

Read the full file on GitHub · 736 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 736 lines · 0 tokens per session scan A 23861c016b6c

Subscribe to this mod's changes

sql-optimization is a skill published in the GitHub repository stefan-jansen/claude-code-toolkit (85 stars, last pushed 1mo ago), licensed MIT. It adds 78 tokens to every session and 4,573 once invoked, about $0.0004 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.