ProSkillsLibraryFreePassScan
Data & SQL

Data & SQL · agent skill

Query Optimizer

EXPLAIN-driven tuning with before and after timings.

What it does

Loads when it is needed

Your agent loads it when a query or page is slow. It carries 5 rules and reads a profile.md with your details before it starts, so the output matches your brand and project.

Same prompt, with and without

Slow account events feed

Prompt · This events feed query is taking about 1.9s p95 and gets hit on every dashboard load. Here's the plan. Can you fix it?

SELECT id, kind, payload_size, created_at FROM events
WHERE account_id = 8841 AND kind = 'invoice.paid' AND created_at >= now() - interval '30 days'
ORDER BY created_at DESC LIMIT 50;

Limit (actual time=1893.4..1893.5 rows=50 loops=1)
  -> Sort (actual time=1893.4..1893.4 rows=50) Sort Key: created_at DESC, Method: top-N heapsort
    -> Gather (workers launched 2)
      -> Parallel Seq Scan on events (actual time=0.9..1871.2 rows=1480 loops=3)
           Filter: (account_id = 8841 AND kind = 'invoice.paid' AND created_at >= ...)
           Rows Removed by Filter: 12664000
Buffers: shared hit=1204 read=412880
Execution Time: 1894.1 ms
Table events: 38M rows. Indexes: events_pkey, idx_events_created_at. ~250 inserts/s. Called ~40x/min. Measured on a replica.

Profile used: Postgres 16, Prisma, 38M-row events table; snake_case, idx_<table>_<cols> index names

Without the skill
The plan scans the whole table (about 38M rows) to find ~4.4k matches and then sorts. You need a composite index matching the filter and sort: CREATE INDEX CONCURRENTLY idx_events_account_id_kind_created_at ON events (account_id, kind, created_at DESC); Equality columns first (account_id, kind), then created_at. Postgres can then walk the index in order, stop after 50 rows and skip the sort entirely, so the query should only touch a few dozen index entries and heap rows instead of the whole table. If you want an index-only scan, add INCLUDE (id, payload_size); that makes the index bigger, so only do it if the heap fetches turn out to matter. Prisma notes: - CONCURRENTLY can't run inside a transaction, so put it alone in its own migration file (hand-edit the generated SQL), or run it manually and mark the migration applied with prisma migrate resolve. - Declaring @@index([accountId, kind, createdAt(sort: Desc)]) in schema.prisma keeps the schema in sync. Match the name with map: "idx_events_account_id_kind_created_at". Costs: at ~250 inserts/s one extra index is cheap but not free (more disk and write work). Keep idx_events_created_at unless other queries need it; check pg_stat_user_indexes and drop it later if unused. Run it on the primary (it replicates), then re-run EXPLAIN (ANALYZE, BUFFERS) to confirm you get an Index Scan with no Sort node.
With the skill
Query: account events feed Database: Postgres 16 Measured on: replica (plan supplied) Before: 1,894 ms, Parallel Seq Scan, 38.0M rows read, 4,440 kept, 412,880 buffers read After: not measured, I cannot reach your replica. Expected plan: Index Scan using idx_events_account_kind_created, about 50 rows read, no Sort node. Please paste EXPLAIN (ANALYZE, BUFFERS) after the build. Hypothesis: no index leads with account_id and kind, so every call scans the whole table to keep 0.01% of it; the sort is only 50 rows and is not the problem. One change (equality, equality, then the range and sort column): CREATE INDEX CONCURRENTLY idx_events_account_kind_created ON events (account_id, kind, created_at DESC); Put it in its own hand-edited Prisma migration file, since a concurrent build cannot run in a transaction. Add the matching @@index afterwards so schema.prisma does not drift. Check indisvalid; if false, drop concurrently and retry. Rollback: DROP INDEX CONCURRENTLY idx_events_account_kind_created; Cost: one more index on a table taking 250 inserts/s; size it with pg_relation_size after the build. Not a duplicate or prefix of idx_events_created_at. Other queries on events are unchanged. Follow-ups: 40 calls/min at 1.9 s is the real load; re-check p95 after the change before touching anything else.

Both answers from the same model (Claude sonnet), 2026-10-16. The one without the skill was written in a separate session that never saw it. Shortened where marked with …

Install

One command, in your project

It detects your agent; add --agent all for every agent.

npx proskills-cli add query-optimizer --order <order id from your receipt>

Or copy the skill folder (the one holding SKILL.md) into the directory your agent reads, then restart it or start a new session.

AgentThis projectEvery project
Claude Code.claude/skills/~/.claude/skills/
Cursor.agents/skills/ (or .cursor/skills/)~/.cursor/skills/
Codex.agents/skills/~/.agents/skills/
Gemini CLI.agents/skills/ (or .gemini/skills/)~/.gemini/skills/

Changelog

What changed

  • v1.0.0 · 2026-10-16
    • First versioned release: EXPLAIN-driven tuning with before and after timings.
    • Scanned 100/100 before release.

Keep it current with npx proskills-cli update; your profile.md is never touched. Everyone who bought it gets the update free and an email when it lands.

Same collection

More from Data & SQL

The scan checks spec compliance, token cost, clarity and security patterns. A clean scan is not a guarantee: static scanners can be evaded. Scan any skill yourself.