/polymath-data:sql-optimizefrom polymath-data
Read EXPLAIN ANALYZE output and propose targeted optimizations — index, predicate, join order, or query shape changes.
Ships in polymath-data. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
A SQL query is slow. Read the plan, identify the dominant cost, propose one concrete change.
Mode: analytical — judgment inside a fixed frame. Keep the output shape; every verdict cites its evidence; unknowns are marked [TBD] or [VERIFY], never filled in.
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) on Postgres (or the dialect equivalent). Run with a warmed cache when comparing.EXPLAIN ANALYZE executes the query. For INSERT/UPDATE/DELETE, wrap it in BEGIN; … ROLLBACK;. Don't run a heavy query's ANALYZE on a production primary without the owner's OK — use a replica or a copy, or start with plain EXPLAIN.
The plan format differs, but the questions are the same: which node dominates, what does it scan, why was that picked, what could change it. Cheat sheet:
| Dialect | Command | Key fields to read |
|---|---|---|
| Postgres | EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) <query> | cost, actual time, rows, loops, Buffers: shared hit / read, Sort Method: external merge |
| MySQL ≥ 8 | EXPLAIN ANALYZE <query> or EXPLAIN FORMAT=JSON <query> | actual_time_first, actual_time_last, rows_examined_per_scan, using_temporary_table, using_filesort |
| SQLite | EXPLAIN QUERY PLAN <query> | SCAN vs SEARCH, USING INDEX / USING COVERING INDEX |
| BigQuery | EXECUTION DETAILS tab or INFORMATION_SCHEMA.JOBS_BY_*.query_info | slot_ms, shuffle output bytes, repartitioned input rows, stage compute ratio max/avg (skew) |
| Snowflake | EXPLAIN <query> and the Query Profile UI | bytes scanned, partitions scanned/total, spilling to local/remote storage, cardinality estimate vs actual |
| Redshift | EXPLAIN <query> + SVL_QUERY_REPORT | step type, rows_pre_filter vs rows, is_diskbased, distribution / sort key alignment |
| DuckDB | EXPLAIN ANALYZE <query> | actual time, cardinality, function (operator) names |
In every dialect the root node is the final operator and its children
are inputs: start at the leaves (most indented) and follow rows upward.
In Postgres, a node's actual time includes its children and is per
loop — multiply by loops, then subtract the children to get the
node's own cost. The dominant cost rule is dialect-independent: pick
the single node whose own time is > 50 % of runtime; if no node
dominates, pick the deepest expensive one.
Rows Removed by Filter relative to rows returned → the predicate isn't using an index (or the index isn't selective enough).WHERE lower(email) = ...) blocking index use → use a functional index or normalize on write.EXPLAIN ANALYZE of the patched query.Slow query: top countries by active users.
Plan summary:
- Total: 4,200 ms.
- Dominant: Seq Scan on events (3,800 ms, 12M rows, returns 480k).
- Predicate: created_at >= now() - interval '7 days' AND country_code IS NOT NULL.
Diagnosis:
events.created_at is indexed but the planner picked Seq Scan because
the optimizer's row estimate for the predicate is off by 10x.
First fix:
ANALYZE events;
Re-run EXPLAIN. If the planner still picks Seq Scan, force-test with
SET enable_seqscan = off;
to confirm the index would be cheaper.
Likely permanent fix:
Composite index events(created_at, country_code).
Drop the standalone events(country_code) index if unused.
sys.schema_unused_indexes,
BigQuery's "this query did not use the partition / cluster column"
warning) for unused ones first.polymath-backend:migration-plan
— when the proposed fix is a schema change (new index, new
composite key, change of partition / sort column), plan it as an
online migration first.polymath-backend:review-migration
— Postgres-specific lock review before shipping the index change.polymath-data:sql-write
— when the right fix is rewriting the query rather than the schema.polymath-database:db-performance-triage — when you don't yet know which query or resource is the problem; it finds the statement, this skill reads its plan.polymath-database:index-strategy — designing the index set for a whole workload rather than one query; polymath-database:postgres-performance for Postgres statistics, vacuum, and HOT effects.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub mohammadbafkar/polymath --plugin polymath-data4 skills in this plugin
/metrics-treeDecompose a North Star metric into a tree of input metrics so each team can see the lever they own; output is a tree, not a dashboard./run-experimentPlan an A/B (or A/B/n) experiment — hypothesis, primary + guardrail metrics, MDE + sample size + duration, randomization, stop conditions./sql-writeWrite a SQL query that answers a specific question; parameterized, dialect-aware, readable, with the assumed schema documented inline.Other plugins' skills for the same job