loom-sql-optimization

loom-sql-optimization is a skill for Claude Code, Codex from cosmix/loom. It costs 16 tokens per session (7,008 once invoked), scanned A, original, MIT.

A method for making SQL queries run more efficiently, especially in PostgreSQL, a database system. It uses execution plans, indexes, query changes, and production-like validation.

In plain words
What is it for?
It is for finding slow queries, reading EXPLAIN output, designing indexes, rewriting queries, and checking that a change improves performance without changing results.
Why use it?
It replaces guesswork with evidence about how the database actually runs a query and where time or work is being spent.

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/cosmix/loom/loom-sql-optimization
Any agent
npx skills add cosmix/loom --skill loom-sql-optimization
Clone the repo
git clone --depth 1 https://github.com/cosmix/loom

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

README.md
[![agentmods](https://agentmods.dev/badge/skills/cosmix/loom/loom-sql-optimization.svg)](https://agentmods.dev/skills/cosmix/loom/loom-sql-optimization)
Your own site
<a href="https://agentmods.dev/skills/cosmix/loom/loom-sql-optimization"><img src="https://agentmods.dev/badge/skills/cosmix/loom/loom-sql-optimization.svg" alt="Measured on agentmods" height="20"></a>
Per session 16 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 7,008 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.00016 $0.07008
Opus 5 $0.00008 $0.03504
Sonnet 5 $0.00003 $0.01402
Haiku 4.5 $0.00002 $0.00701

Measured yesterday against content hash 83142f745312, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

loom-sql-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 yesterday.

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/loom-sql-optimization/SKILL.md · 545 lines

How it starts

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

SQL Optimization

Overview

Analyzing and tuning SQL for performance: reading execution plans, index design, query rewriting, and PostgreSQL-specific behavior. Most notes assume PostgreSQL. The mechanism-level material — reading EXPLAIN, composite-index column order, partial-index limits, statistics, concurrency — is in Expert Practices below; this section is the workflow and the anti-pattern catalogue.

Workflow

  1. Find the slow query (logs, pg_stat_statements by total time, not just per-call).
  2. Explain it: EXPLAIN (ANALYZE, BUFFERS, SETTINGS). Read plans by estimated-vs-actual row divergence (bad stats → wrong join/scan choice), scan type, join algorithm, and Rows Removed by Filter. See Expert Practices → Reading EXPLAIN.
  3. Fix in priority order: refresh/extend statistics → add/reshape an index → rewrite the query → denormalize/derive → tune config. Change one thing at a time.
  4. Validate: re-EXPLAIN on production-like data, confirm the target node changed (Sort gone / Index Scan chosen / Heap Fetches low), verify correctness, monitor post-deploy.

Best Practices

  • EXPLAIN (ANALYZE, BUFFERS) is the source of truth — never optimize by guessing; actual rows and buffers reveal the real cost.
  • Select only needed columns (enables index-only scans; avoids TOAST/wide-row I/O).
  • Index for WHERE/JOIN/ORDER BY, but mind column order and the write tax (Expert Practices → Index Design).
  • Avoid N+1: one JOIN or a batched WHERE id = ANY($1), not a query per row.
  • NOT EXISTS, never NOT IN (nullable subquery) — one NULL silently returns zero rows; NOT EXISTS is NULL-safe and hash-anti-join-able. (Inclusion IN/ANY/EXISTS plan identically in modern PG — pick readability.)
  • No functions/implicit casts on indexed columns (WHERE lower(x)= needs an expression index; WHERE int_col = '1' may cast and skip the index).
  • Keyset pagination, not deep OFFSET (Expert Practices).
  • CTEs are not fences (PG12+): a non-recursive, side-effect-free CTE used once is inlined; used >1× it materializes. Force with AS MATERIALIZED / AS NOT MATERIALIZED; the OFFSET 0 trick is obsolete.
  • ANALYZE after bulk loads; monitor query performance over time.

Read the full file on GitHub · 545 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. yesterday First seen · 545 lines · 16 tokens per session scan A 83142f745312

Subscribe to this mod's changes

loom-sql-optimization is a skill published in the GitHub repository cosmix/loom (54 stars, last pushed yesterday), licensed MIT. It adds 16 tokens to every session and 7,008 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-09-03.

Related

Other skills, from other repositories

postgresql-review

PostgreSQL query review, optimalisering og beste praksis for Nav-applikasjoner.

navikt/copilot · 21 tokens

database-architecture

MANDATORY when designing schemas, writing migrations, creating indexes, or making architectural database decisions - enforces PostgreSQL 18 best practices including AIO, UUIDv7, temporal constraints, and modern indexing strategies.

troykelly/claude-skills · 45 tokens

timescaledb

MANDATORY when working with time-series data, hypertables, continuous aggregates, or compression - enforces TimescaleDB 2.24.0 best practices including lightning-fast recompression, UUIDv7 continuous aggregates, and Direct Compress.

troykelly/claude-skills · 50 tokens

postgis

MANDATORY when working with geographic data, spatial queries, geometry operations, or location-based features - enforces PostGIS 3.6.1 best practices including STCoverageClean, SFCGAL 3D functions, and bigint topology.

troykelly/claude-skills · 52 tokens

postgres-rls

MANDATORY when touching auth tables, tenant isolation, RLS policies, or multi-tenant database code - enforces Row Level Security best practices and catches common bypass vulnerabilities.

troykelly/claude-skills · 38 tokens

alloydb-basics

Manages clusters, instances, and backups for AlloyDB for PostgreSQL, and integrates with AlloyDB Model Context Protocol (MCP) tools for automated database operations. Use when creating, configuring, or administering AlloyDB databases. Do NOT use for general PostgreSQL instances (e.g. Cloud SQL) or other GCP databases.

google/skills · 72 tokens