SQL Query Optimizer

A review and rewriting workflow for SQL, the language used to read and change data in databases.

In plain words
What is it for?
It is for examining indexes, joins, subqueries, and N+1 query patterns, then comparing the original query with a revised version and explaining the changes.
Why use it?
It helps find query patterns that make databases slow, such as unnecessary full-table reads, inefficient joins, and repeated queries.

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/notysoty/openagentskills/sql-query-optimizer
Any agent
npx skills add Notysoty/openagentskills --skill sql-query-optimizer
Clone the repo
git clone --depth 1 https://github.com/Notysoty/openagentskills

Made for: Claude Code, Codex.

Per session 20 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,362 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.00020 $0.01362
Opus 5 $0.00010 $0.00681
Sonnet 5 $0.00004 $0.00272
Haiku 4.5 $0.00002 $0.00136

Measured 2d ago against content hash 7ff2863adcb7, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

SQL Query Optimizer 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 2d 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/sql-query-optimizer/SKILL.md · 143 lines

How it starts

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

SQL Query Optimizer

What this skill does

This skill directs the agent to analyze a SQL query for common performance anti-patterns and rewrite it to execute more efficiently. It checks indexes, join strategies, subquery usage, N+1 patterns, unnecessary full table scans, and more — then provides a side-by-side comparison of the original and optimized query with a plain-English explanation of each change.

Use this when a query is running slowly in production, when you're writing a complex query for the first time and want a second opinion, or when you're reviewing a pull request that touches database queries.

How to use

Claude Code / Cursor / Codex

Copy this file to .agents/skills/sql-query-optimizer/SKILL.md in your project root (for Claude Code), or add the instructions below to your .cursorrules (for Cursor).

Then ask:

  • "Optimize this query using the SQL Query Optimizer skill."
  • "This query takes 3 seconds on 500k rows. Use the SQL Query Optimizer skill to improve it."

Provide the query, and optionally:

  • The database system (PostgreSQL, MySQL, SQLite, etc.)
  • Relevant table schemas and row counts
  • Any existing index definitions
  • The EXPLAIN or EXPLAIN ANALYZE output if available

The Prompt / Instructions for the Agent

When asked to optimize a SQL query, follow these steps:

  1. Identify the database system (PostgreSQL, MySQL, SQLite, MSSQL). Syntax and optimizer behavior differ. If not specified, ask.

  2. Analyze for these anti-patterns in order:

    • Missing indexes: Are columns in WHERE, JOIN ON, ORDER BY, or GROUP BY clauses indexed? Flag unindexed columns on large tables.
    • *SELECT : Does the query select all columns when only a few are needed? Unnecessary columns increase I/O and memory.
    • N+1 queries: Is this query run inside a loop in application code (indicated by a comment or context)? Suggest a single JOIN or subquery instead.
    • Correlated subqueries: Does the WHERE clause contain a subquery that references the outer query? These execute once per row. Rewrite as a JOIN or CTE.
    • Non-sargable predicates: Are functions applied to indexed columns in WHERE (e.g., WHERE YEAR(created_at) = 2024)? This prevents index use.
    • Implicit type coercions: Do joins compare columns of different types (e.g., INT vs VARCHAR)? This forces a full scan.
    • Unnecessary DISTINCT or ORDER BY: Is DISTINCT used where it's not needed? Is ORDER BY applied to a subquery that is later filtered?
    • Large offset pagination: Does the query use LIMIT x OFFSET y with a large offset? Suggest keyset pagination instead.

Read the full file on GitHub · 143 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. 2d ago First seen · 143 lines · 20 tokens per session scan A 7ff2863adcb7

Subscribe to this mod's changes

SQL Query Optimizer is a skill published in the GitHub repository Notysoty/openagentskills (9 stars, last pushed 19d ago), licensed MIT. It adds 20 tokens to every session and 1,362 once invoked, about $0.0001 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-31.