agentleFS
milad-ahmdmilad-ahmd/agent-skills/skills/sql-optimizer/SKILL.md

sql-optimizer

Skill5 starsChanged 14 days agoMITView on GitHub

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.

8 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use changelog-keeper, 0-template and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • JayRHaSkill

    changelog-keeper

    Maintains a CHANGELOG.md in the Keep a Changelog format with Semantic Versioning, grouping user-facing entries under Added/Changed/Deprecated/Removed/Fixed/Security and cutting dated releases from an Unreleased section. Use this skill when the user asks to "update the changelog", "add a changelog entry", "cut a release", "bump the version", "what changed since the last release", or to create/clean up a CHANGELOG.md or release notes.

    Also in 4 projects that use this

  • JayRHaSkill

    0-template

    Template for creating a new Agent Skill. Copy this folder, rename it to your skill's kebab-case name, and fill in every section. Use this as the starting point whenever you author a new skill for the AgentSkills library.

    Also in 3 projects that use this

  • xmqywxSkill

    meeting-summarizer

    Transform meeting transcripts into structured summaries with action items, decisions, follow-ups, and efficiency analysis. Works with any meeting format.

    Also in 3 projects that use this

  • MehdibargachSkill

    okr-writer

    Turn business goals into structured OKRs with measurable Key Results, linked initiatives, alignment check, and anti-goals. Takes a strategic priority and outputs ready-to-use OKRs.

    Also in 3 projects that use this

  • pbc-osSkill

    secrets-manager

    Set up and configure cloud secrets management for your AI agent. Guides users through platform selection (GCP, AWS, Azure, 1Password, Doppler, HashiCorp Vault), account setup, CLI configuration, and secure credential storage.

    Also in 3 projects that use this

  • +7 more from JayRHa

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 8 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Prefer query rewrites before adding indexes.

    line 38
  2. Very specific:

    Consider partial indexes (WHERE deleted_at IS NULL), covering indexes (INCLUDE) and expression indexes.

    line 39
  3. Very specific:

    Use CONCURRENTLY (Postgres) or online DDL for production tables, and ship index changes as a migration in the project's migration tool.

    line 41

“Common” is measured: kept by at least 6 in 10 of the 8 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

SQL 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 table in Postgres, SHOW INDEX FROM t in 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 in BEGIN; ... ROLLBACK;.
    • MySQL: EXPLAIN ANALYZE <query> (8.0.18+) or EXPLAIN FORMAT=JSON.
    • SQLite: EXPLAIN QUERY PLAN <query>.

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', OR across different columns, or NOT IN with NULLs.
  • Large estimate-vs-actual row mismatches → stale statistics (ANALYZE).
  • SELECT * on wide tables, unnecessary DISTINCT, or ORDER BY with no supporting index plus LIMIT.
  • Deep OFFSET pagination → 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.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.