agentleFS
tranhieutttranhieutt/software_development_department/.claude/skills/sql-optimization-patterns/SKILL.md

sql-optimization-patterns

Skill71 starsChanged 7 months agoMITView on GitHub

Provides SQL optimization patterns for query performance, indexing strategies, schema design, and database tuning. Use when optimizing slow queries, designing indexes, or tuning database performance.

23 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use team-composition-analysis, similarity-search-patterns 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.
  • xfstudioSkill

    team-composition-analysis

    This skill should be used when the user asks to "plan team

    Also in 6 projects that use this

  • xfstudioSkill

    similarity-search-patterns

    Implement efficient similarity search with vector databases. Use when building semantic search, implementing nearest neighbor queries, or optimizing retrieval performance.

    Also in 7 projects that use this

  • +10 more from xfstudio

The file

We split this skill into the rules it gives an agent

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

  • 0

    Common

    Most of the 22 edited versions we read keep these.

  • 3

    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. Distinctive:

    CREATE INDEX idx_orders_user_id ON orders(user_id).

    line 21
  2. Distinctive:

    CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC).

    line 24
  3. Very specific:

    CREATE INDEX idx_orders_covering ON orders(user_id, status) INCLUDE (total, created_at).

    line 27
  4. Distinctive:

    Write-heavy tables with rare reads

    line 33

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

Query optimization, indexing, and performance tuning for PostgreSQL, MySQL, and SQLite.

Index Strategy

When to Create Index

```sql -- High selectivity columns (many unique values) CREATE INDEX idx_orders_user_id ON orders(user_id);

-- Composite index: order matters (equality first, then range) CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC);

-- Covering index (includes all needed columns) CREATE INDEX idx_orders_covering ON orders(user_id, status) INCLUDE (total, created_at); ```

When NOT to Index

  • Low cardinality columns (boolean, status with few values)
  • Small tables (< 1000 rows)
  • Write-heavy tables with rare reads

Query Patterns

Avoid SELECT *

```sql -- Bad SELECT * FROM orders WHERE user_id = 1;

-- Good (select only needed columns) SELECT id, total, status FROM orders WHERE user_id = 1; ```

Avoid N+1 (use JOIN or subquery)

```sql -- Bad: N+1 queries from application -- Good: Single query with JOIN SELECT o.id, o.total, u.name FROM orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'pending'; ```

Pagination (keyset, not OFFSET)

```sql -- Bad: OFFSET scans all skipped rows SELECT * FROM orders ORDER BY id LIMIT 20 OFFSET 10000;

-- Good: Keyset pagination SELECT * FROM orders WHERE id > 10000 ORDER BY id LIMIT 20; ```

EXPLAIN ANALYZE

```sql EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 1 AND status = 'pending'; ```

Read output:

  • Seq Scan = missing index
  • Index Scan = good
  • Nested Loop with high row count = check join strategy

Schema Anti-Patterns

Anti-PatternProblemFix
EAV (Entity-Attribute-Value)No type safety, slow queriesUse JSONB or proper columns
God tableToo many columnsNormalize into related tables
No constraintsData integrity issuesAdd CHECK, FK, UNIQUE constraints
String datesSorting/filtering issuesUse TIMESTAMP type

Connection Pooling

``` App → Pool (min: 5, max: 20) → PostgreSQL ```

Tools: PgBouncer (PostgreSQL), ProxySQL (MySQL).

Related Skills

  • database-architect — schema design
  • postgres-patterns — PostgreSQL specifics
  • nosql-expert — NoSQL alternatives
  • db-review — database code review

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.