sql-query-optimization

sql-query-optimization is a skill for Claude Code, Codex from soulcodex/agentic. It costs 70 tokens per session (902 once invoked), scanned A, original, MIT.

A workflow for finding and fixing slow SQL queries. SQL is the language used to retrieve and change data in relational databases; EXPLAIN ANALYZE shows how a database executes a query and where time or resources are spent.

In plain words
What is it for?
Use it to diagnose bottlenecks such as full-table scans or inaccurate row estimates, improve indexes and query structure, and confirm database performance gains.
Why use it?
It replaces guesswork with a measured baseline, execution-plan analysis, targeted indexes or rewrites, and verification of the result.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one. Also seen: mentions Codex; mentions OpenCode.

Good fit Use it to diagnose bottlenecks such as full-table scans or inaccurate row estimates, improve indexes and query structure, and confirm database performance gains.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/soulcodex/agentic/sql-query-optimization
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 soulcodex/agentic --skill sql-query-optimization
Clone the repo
git clone --depth 1 https://github.com/soulcodex/agentic

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-optimization

README.md
[![agentmods](https://agentmods.dev/badge/skills/soulcodex/agentic/sql-query-optimization/github.svg)](https://agentmods.dev/skills/soulcodex/agentic/sql-query-optimization)
Your own site
<a href="https://agentmods.dev/skills/soulcodex/agentic/sql-query-optimization"><img src="https://agentmods.dev/badge/skills/soulcodex/agentic/sql-query-optimization/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-optimization

Your own site · 80×15
<a href="https://agentmods.dev/skills/soulcodex/agentic/sql-query-optimization"><img src="https://agentmods.dev/badge/skills/soulcodex/agentic/sql-query-optimization.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 70 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 902 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. Third-party audits
  • NVIDIA SkillSpector pass 7 Sept 2026
How audits are shown
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.00070 $0.00902
Opus 5.5 $0.00028 $0.00361
Sonnet 5.5 $0.00014 $0.00180
Haiku 4.5 $0.00007 $0.00090

Measured 1mo ago against content hash a598cb719967, 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-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 1mo 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.

skills/backend/sql-query-optimization/SKILL.md · 117 lines

How it starts

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

SQL Query Optimization Skill

Step 1 — Reproduce the Slow Query

Establish a reproducible baseline before making any changes:

  • Capture the exact query (including parameter values if possible).
  • Note the current execution time (p95 from APM, or run it manually 3–5 times).
  • Confirm the database engine (PostgreSQL, MySQL, SQLite, etc.) and version.

Step 2 — Run EXPLAIN (ANALYZE, BUFFERS)

PostgreSQL:

EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT ...;  -- paste the full query here

MySQL:

EXPLAIN FORMAT=JSON
SELECT ...;

Capture the full output before making any changes.

Step 3 — Identify the Bottleneck

Read the plan top-down (outermost → innermost). Look for:

Signal What it means
Seq Scan on a large table No index — or the planner chose not to use one
Rows estimate far from actual Stale statistics — run ANALYZE <table>
Hash Join with a huge hash table May need a nested-loop + index for small inputs
High Buffers: shared hit Heavily cached — latency is CPU-bound, not I/O
High Buffers: shared read I/O bound — consider indexes or read replicas
Bitmap Heap Scan with many rows Covering index may eliminate heap fetches

Step 4 — Fix

Add an index (most common fix):

-- B-tree for equality and range
CREATE INDEX CONCURRENTLY ON orders(user_id);

-- Partial index for common filter
CREATE INDEX CONCURRENTLY ON orders(created_at)
  WHERE status = 'pending';

-- Covering index to eliminate heap fetch
CREATE INDEX CONCURRENTLY ON orders(user_id)
  INCLUDE (status, total_amount);

-- Expression index for function-wrapped column
CREATE INDEX CONCURRENTLY ON users(lower(email));

Always use CONCURRENTLY in production to avoid table locks.

Rewrite the query (when the plan is structurally wrong):

  • Replace correlated subqueries with JOIN or EXISTS.
  • Replace SELECT * with specific columns needed.
  • Replace OFFSET pagination with keyset pagination:
    -- Instead of: LIMIT 20 OFFSET 10000
    WHERE id > :last_seen_id ORDER BY id LIMIT 20
    
  • Replace IN (SELECT ...) with EXISTS (SELECT 1 FROM ...) for large subqueries.

Read the full file on GitHub · 117 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. 1mo ago First seen · 117 lines · 70 tokens per session scan A a598cb719967

Subscribe to this mod's changes

sql-query-optimization is a skill published in the GitHub repository soulcodex/agentic (10 stars, last pushed 28d ago), licensed MIT. It adds 70 tokens to every session and 902 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-08-31.

Related

Other skills, from other repositories

database-connections

Connect a Mendix app to an external database over JDBC with the External Database Connector, and define the queries microflows run against it. Use when the app must read or write Oracle, PostgreSQL, MySQL or SQL Server directly from a microflow.

mendixlabs/mxcli · 55 tokens

connect-rapidminer-graph

Fetch data from a RapidMiner graph mart or any SPARQL 1.1 HTTP endpoint (AnzoGraph and friends) and surface it as Mendix entities. Use when a graph database with a SPARQL endpoint has to feed a Mendix app read-only.

mendixlabs/mxcli · 62 tokens

mendix-odata-pushdown

Push OData query options into the SQL of a Mendix resource served by a read microflow, so $filter, $orderby, $top, $skip, $count and the key lookup reach the database instead of being silently dropped. Use when publishing an OData resource over data Mendix has no table for — a warehouse view, a legacy database, a…

mendixlabs/mxcli · 117 tokens

generate-domain-model

Generate a complete Mendix domain model in MDL — entities, attributes, associations, enumerations — and validate it. Use when asked for a domain model for a business area (e-commerce, HR, CRM, …) rather than a single entity.

mendixlabs/mxcli · 55 tokens

mendix-bulk-oql-dml

Run set-based INSERT, UPDATE and DELETE against Mendix entities through OQL statements, which the runtime supports and Studio Pro cannot author. Use when a microflow would otherwise retrieve a large list and loop — applying rules across a table, copying a year of records, staging and promoting an import, archiving…

mendixlabs/mxcli · 98 tokens

system-design

Production system design — scalability patterns, trade-off analysis, database selection, caching strategy, API design, and architecture decisions.

chandrudp29/skillhub · 26 tokens