query-rewriter
Skill0 starsChanged 39 days agoMITView on GitHub
Rewrites a structurally inefficient SQL query into a faster equivalent that returns identical results - correlated subqueries to joins or LATERAL, accidental cross joins to explicit ON predicates, OR-across-columns to UNION, SELECT * to projected columns, and deep OFFSET pagination to keyset. Use when a query is slow because of its shape rather than its indexes - EXPLAIN shows a Cartesian blowup, a per-row subquery, or OFFSET discarding tens of thousands of rows - and the indexes are already in place. Do NOT use when the fix is adding or reordering an index - use index-advisor instead; do NOT use to diagnose an unknown slow query from its plan - use sql-query-optimizer instead.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- huifer/CloudSavertheir own version, first seen Jun 13, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- huifer/CloudSaver★ 2same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository7 other files it gives its agents.
- connection-pool-tunerSkill
- explain-plan-readerSkill
- index-advisorSkill
- migration-safety-checkerSkill
- n-plus-one-hunterSkill
- partition-plannerSkill
- replication-lag-debuggerSkill
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 7 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈7
Distinctive · estimate
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
- Distinctive: line 28
Prove equivalence: Run the original and the rewrite against the same data and diff the full output, including ordering and row count.
- Distinctive: line 66
Every rewrite removes a cost driver visible in EXPLAIN, not a suspected one.
- Distinctive: line 68
Every LEFT-vs-INNER, DISTINCT, and UNION-vs-UNION ALL choice is explicitly justified against multiplicity and NULL behavior.
- Distinctive: line 72
Do NOT recommend creating or reordering an index - that is index-advisor's job.
- Distinctive: line 73
Do NOT rewrite a query the planner already optimizes.
- Distinctive: line 74
Do NOT trade correctness for speed - never ship a rewrite whose output has not been diffed against the original.
- Very specific: line 75
Do NOT use NOT IN with a subquery that can yield NULLs.
- Distinctive: line 76
Do NOT convert an outer join to an inner join (or EXISTS to JOIN) without checking whether unmatched rows or duplicate multiplicity change …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
SQL Query Rewriter
Reshape a slow query into a faster equivalent that returns identical rows. Shape beats tuning when the query does avoidable work - but a rewrite that changes results is worse than a slow query, so equivalence is proven, never assumed. This skill owns the structural rewrite only: index choice belongs to index-advisor, and plan diagnosis belongs to sql-query-optimizer.
Inputs to collect
- The query text and its
EXPLAIN (ANALYZE)output - ANALYZE if the query can be run safely, plain EXPLAIN otherwise (label conclusions from estimates as provisional). - Approximate row counts of each table involved, to spot Cartesian blowups.
- The existing indexes on the touched tables - a rewrite often assumes a supporting index; confirm it exists or hand off to index-advisor.
- What the caller actually consumes: which columns, whether order matters, whether duplicates are possible.
- A way to run both versions against the same data for the equivalence diff.
Workflow
- Confirm the shape is the problem. Read the EXPLAIN. Only rewrite if the plan shows a structural cost driver: actual rows far exceeding any single table (Cartesian product), a subquery re-executed per outer row (
SubPlanwith high loop counts), a Sort/Limit after scanning a huge OFFSET, or an OR that forces a seq scan. If the plan is already optimal, stop - modern Postgres unnests many subqueries itself. - Fix accidental cross joins first. A FROM listing N tables with a missing or incomplete join predicate multiplies row counts. Write one explicit
JOIN ... ONper table relationship. If actual rows dwarf every source table, suspect a dropped ON clause. - Decorrelate per-row subqueries. A scalar subquery in SELECT/WHERE that references the outer row runs once per row. Rewrite it as a JOIN to a grouped derived table; in Postgres use a
LATERALjoin for per-row top-N. ReplaceWHERE EXISTSchains with a single JOIN where multiplicity allows. KeepEXISTSoverINwhen the inner set is large - EXISTS short-circuits andNOT INreturns wrong results on NULLs. - Split OR across different columns.
WHERE a = 1 OR b = 2can use neither column cleanly. Rewrite asUNIONof two single-predicate queries (UNION ALLplus dedup only if duplicates are possible). An OR on the same column collapses toIN (...). - Project explicit columns. Replace
SELECT *with the exact columns the caller uses. This shrinks I/O and lets an existing covering index answer the query without a heap fetch. - Convert deep OFFSET to keyset pagination.
OFFSET 100000 LIMIT 20scans and throws away 100k rows and degrades per page. Rewrite as keyset:WHERE (created_at, id) < (:last_ts, :last_id) ORDER BY created_at DESC, id DESC LIMIT 20. Keep OFFSET only for small, bounded sets where a jump-to-page-N UI is required. - Prove equivalence. Run the original and the rewrite against the same data and diff the full output, including ordering and row count. Re-check EXPLAIN to confirm the structural cost driver is gone.
Worked example: decorrelating a per-row subquery
Bad - latest order date per customer via a correlated scalar subquery:
SELECT c.id, c.name,
(SELECT MAX(o.created_at) FROM orders o
WHERE o.customer_id = c.id) AS last_order
FROM customers c
WHERE c.region = 'EMEA';EXPLAIN shows the cost driver: SubPlan 1 under the customers scan with loops=48213 - the orders aggregate re-executes once per EMEA customer, ~48k index probes.
Good - aggregate once, join once:
SELECT c.id, c.name, o.last_order
FROM customers c
LEFT JOIN (
SELECT customer_id, MAX(created_at) AS last_order
FROM orders
GROUP BY customer_id
) o ON o.customer_id = c.id
WHERE c.region = 'EMEA';EXPLAIN now shows one HashAggregate over orders and a single Hash Left Join - the per-row loop is gone. The LEFT JOIN (not INNER) preserves customers with no orders, matching the original's NULL behavior; the equivalence diff confirms identical rows. If only a few customers are EMEA, the LATERAL variant (LEFT JOIN LATERAL (SELECT MAX(...) FROM orders o WHERE o.customer_id = c.id) ...) probes per row but only for the filtered set - pick whichever EXPLAIN proves cheaper on the real data.
Deliverable
Produce the rewritten query plus a rewrite note containing: the structural cost driver quoted from the original EXPLAIN, the before/after plan comparison showing that driver removed, the equivalence evidence (row-count and full-output diff, including NULL and ordering behavior), and any index the rewrite assumes - with a hand-off to index-advisor if it does not exist.
Quality bar
- Output is identical to the original on the same data - same rows, same order, same NULL handling.
- Every rewrite removes a cost driver visible in EXPLAIN, not a suspected one.
- The rewrite is justified by the plan, not by folklore; if EXPLAIN does not change, the rewrite is not done.
- Every LEFT-vs-INNER, DISTINCT, and UNION-vs-UNION ALL choice is explicitly justified against multiplicity and NULL behavior.
Do NOT
- Do NOT recommend creating or reordering an index - that is index-advisor's job. State that a rewrite assumes the supporting index exists, and hand off if it does not.
- Do NOT rewrite a query the planner already optimizes; check EXPLAIN before refactoring.
- Do NOT trade correctness for speed - never ship a rewrite whose output has not been diffed against the original.
- Do NOT use
NOT INwith a subquery that can yield NULLs; useNOT EXISTS. - Do NOT convert an outer join to an inner join (or EXISTS to JOIN) without checking whether unmatched rows or duplicate multiplicity change the result.
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
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.

