sql-query-performance

sql-query-performance is a skill for Claude Code, Codex from tmj-90/gaffer. It costs 82 tokens per session (1,474 once invoked), scanned A, original, Apache-2.0.

A method for finding and fixing slow database queries, which are commands that read or change stored data. It uses the database’s query plan and before-and-after measurements on realistic data.

In plain words
What is it for?
Use it for timeouts, expensive reports, slow pages, N+1 query problems, index changes, and other database performance work.
Why use it?
A query may be slow because it scans too many rows, makes repeated requests, lacks an index, or sorts inefficiently. Measuring the actual SQL prevents guessing at the fix.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one. Also seen: positional $N argument.

Good fit Use it for timeouts, expensive reports, slow pages, N+1 query problems, index changes, and other database performance work.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/tmj-90/gaffer/sql-query-performance
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.

Any agent
npx skills add tmj-90/gaffer --skill sql-query-performance
Clone the repo
git clone --depth 1 https://github.com/tmj-90/gaffer

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for sql-query-performance

README.md
[![agentmods](https://agentmods.dev/badge/skills/tmj-90/gaffer/sql-query-performance/github.svg)](https://agentmods.dev/skills/tmj-90/gaffer/sql-query-performance)
Your own site
<a href="https://agentmods.dev/skills/tmj-90/gaffer/sql-query-performance"><img src="https://agentmods.dev/badge/skills/tmj-90/gaffer/sql-query-performance/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for sql-query-performance

Your own site · 80×15
<a href="https://agentmods.dev/skills/tmj-90/gaffer/sql-query-performance"><img src="https://agentmods.dev/badge/skills/tmj-90/gaffer/sql-query-performance.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 82 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,474 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
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.1 $0.00082 $0.01474
Opus 5.5 $0.00033 $0.00590
Sonnet 5.5 $0.00016 $0.00295
Haiku 4.5 $0.00008 $0.00147

Measured 16d ago against content hash 95cd3fa8d799, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

sql-query-performance 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 16d ago.

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.

runner/skills/sql-query-performance/SKILL.md · 93 lines

How it starts

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

Make a slow query fast

The database already knows why the query is slow; ask it. Read the plan, find the scan, loop or sort that dominates, change the query or add the index the plan needs, and measure again on data shaped like production. Most slow queries are an N+1, a missing or wrong-order index, a function or type cast on an indexed column, a wide SELECT *, an OFFSET walk, or stale statistics. Sources: PostgreSQL docs ("Using EXPLAIN", indexes, pg_stat_statements, explicit locking); MySQL 8 reference (EXPLAIN ANALYZE, optimisation, InnoDB locking); Markus Winand, Use The Index, Luke.

Steps

  1. Capture the real SQL and its frequency. From the ORM query log, the slow-query log, pg_stat_statements (total and mean time) or performance_schema, with real parameter values. An ORM call is not a query. Count queries per request: fifty fast queries make one slow page. Call search_lore for hot tables and index conventions.
  2. Build production-shaped data. Seed row counts, value distributions and skew comparable to production (generate_series or the repo's fixture factories, the test-fixtures-and-factories skill), then run ANALYZE. A plan on ten rows lies.
  3. Read the plan. Postgres: EXPLAIN (ANALYZE, BUFFERS); MySQL 8.0.18+: EXPLAIN ANALYZE (or EXPLAIN FORMAT=TREE); SQLite: EXPLAIN QUERY PLAN. ANALYZE executes the statement: wrap INSERT/UPDATE/DELETE in BEGIN; … ROLLBACK;. Look for: sequential scans filtering most rows away, nested loops with large outer row counts, sorts or hashes spilling to disk, estimated vs actual rows off by 10× or more (stale or missing statistics; try ANALYZE or extended statistics), and heap fetches on an intended index-only scan (vacuum/visibility map).
  4. Fix N+1 first. Load related rows in one query (join, IN (…)/= ANY($1) batch, the ORM's eager loading or a dataloader) and pin it with a query-count assertion.
  5. Add the index the plan needs, and only that. Composite order: equality columns, then the range column, then the sort columns in the query's direction. Covering (INCLUDE) for hot read paths, partial for selective predicates. One index per access pattern; every index costs every write. Ship it through the migration path (the add-db-migration skill): CREATE INDEX CONCURRENTLY outside a transaction in Postgres (drop and retry if it leaves an INVALID index), online DDL in MySQL.
  6. Make predicates sargable. No function or cast on the indexed column (WHERE created_at >= :day_start AND created_at < :next_day, not date(created_at) = :d), or index the expression; match parameter types to column types (MySQL string-vs-int comparisons defeat indexes); no leading-wildcard LIKE (use trigram or full-text); split OR across different columns into UNION ALL when the plan shows a scan; select only needed columns; replace OFFSET walks with keyset pagination (the pagination-and-filtering skill).
  7. Protect the write path and other sessions. Long reports and bulk updates hold locks and snapshots: set statement_timeout/lock_timeout (or max_execution_time), batch large UPDATE/DELETE in keyed chunks with a commit per chunk, and move heavy reads to a replica, a materialised view refreshed on a schedule (the scheduled-jobs skill; REFRESH MATERIALIZED VIEW CONCURRENTLY needs a unique index), or a summary table maintained by the writes.
  8. Measure again with the same data and tool: before/after plan and timing over repeated runs (discard the first cold run or report cold and warm separately). If the new index does not appear in the plan, drop it.

Read the full file on GitHub · 93 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. 16d ago Changed · +26 lines 95cd3fa8d799
  2. 19d ago First seen · 67 lines · 82 tokens per session scan A 0ca3aa794199

Subscribe to this mod's changes

sql-query-performance is a skill published in the GitHub repository tmj-90/gaffer (2 stars, last pushed 17d ago), licensed Apache-2.0. It adds 82 tokens to every session and 1,474 once invoked, about $0.0003 per session on Opus 5.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-09-28.

Related

Other skills, from other repositories

bulwark-brainstorm

Role-based brainstorming with dual modes: --scoped (sequential Task tool, 5 roles) and --exploratory (Agent Teams peer debate, 4 roles). Use for feasibility assessment and idea validation.

QBall-Inc/the-bulwark · 50 tokens

plan-creation

Create structured implementation plans via a 4-role scrum team (Product Owner, Architect, Eng/Delivery Lead, QA/Critic) with optional Agent Teams peer debate mode.

QBall-Inc/the-bulwark · 38 tokens

anthropic-validator

Validates Claude Code assets (skills, hooks, agents, commands, MCP servers, plugins) against official Anthropic standards. Fetches latest docs dynamically and produces structured validation reports.

QBall-Inc/the-bulwark · 40 tokens

create-subagent

Generates single-purpose Claude Code sub-agents for use via the Task tool. Use when creating dedicated sub-agents, scaffolding agent definitions, or generating agents with diagnostics and permissions setup.

QBall-Inc/the-bulwark · 42 tokens

test-audit

Audit test suites for T1-T4 violations using AST analysis, mock detection, and multi-stage synthesis. Invoke when user asks to audit tests, check test quality, find mock violations, review test effectiveness, or inspect test suites for over-mocking. Triggers automatic rewrites when quality gates fail.

QBall-Inc/the-bulwark · 0 tokens

code-review

Comprehensive code review with distinct aspect based sections. Use when reviewing code, checking for security issues, finding type safety problems, auditing code quality, or when user asks to review code, PRs or changes. Three-phase workflow runs static tools, LLM judgment, and writes diagnostic log.

QBall-Inc/the-bulwark · 61 tokens