agentleFS
FutureJJFutureJJ/claude-skills/skills/sql-optimizer/SKILL.md

sql-optimizer

Skill3 starsChanged 9 months agoMITView on GitHub

SQL query optimization including EXPLAIN analysis, index strategies, window functions, CTEs, query rewriting, and performance tuning. Trigger when users have slow queries, need help reading EXPLAIN output, want index recommendations, or need to write complex analytical queries with window functions or CTEs.

4 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

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

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Get the EXPLAIN plan: Always start with EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT).

    line 12
  2. Distinctive:

    Find the bottleneck: Look for sequential scans on large tables, nested loops with high row counts, sorts on unindexed columns.

    line 13
  3. Distinctive:

    Not running ANALYZE after bulk inserts — planner uses stale statistics

    line 29

“Common” is measured: kept by at least 6 in 10 of the 3 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

You are a database performance expert who reads EXPLAIN plans like a book.

Workflow

  1. Get the EXPLAIN plan. Always start with EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT).
  2. Find the bottleneck. Look for sequential scans on large tables, nested loops with high row counts, sorts on unindexed columns.
  3. Fix the root cause. Usually: missing index, bad join order, or fetching too many rows.

Core Principles

  • Index for your queries, not your schema. Indexes serve query patterns, not table structure.
  • Covering indexes reduce I/O. Include SELECT columns to avoid table lookups.
  • Filter early, join later. Push WHERE conditions close to base tables.
  • Measure before and after. Compare EXPLAIN output, not gut feeling.

Anti-Patterns

  • SELECT * when you need 3 columns — more I/O, blocks covering indexes
  • Functions on indexed columns (WHERE YEAR(created_at) = 2024) — kills index usage
  • OR conditions spanning different columns — use UNION instead
  • Missing LIMIT on exploratory queries — full table scan on millions of rows
  • Not running ANALYZE after bulk inserts — planner uses stale statistics

Reference Guide

TopicReferenceLoad When
EXPLAIN plansreferences/query-plans.mdReading EXPLAIN output, node types
Indexing strategiesreferences/indexing.mdIndex types, composite indexes, partial indexes

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.