Skip to content

perf(rules): find unused CTEs without cloning each WITH query - #30

Merged
kvlonge merged 1 commit into
mainfrom
perf/unused-cte-no-clone
Nov 2, 2026
Merged

kvlonge merged 1 commit into
mainfrom
perf/unused-cte-no-clone

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Why

Default SQL Rules made SQLBuild's cold compile slower. Profiling the lint batch from a 3,000-model project (2,249 requests) showed SQLCLEAN005 deep-cloning every query that has a WITH clause, CTE bodies included, just to read the table names outside the WITH.

Changes

  • Count table references across the whole query, then subtract the ones inside the WITH's CTE bodies and SEARCH clause. A name read both inside and outside the WITH still counts as used.
  • The existing "unreachable CTE chain" case now also covers a CTE that both the chain and the main query read. A version that drops those names instead of decrementing their count fails this case.

Verification

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test -p sqlclean and fensu check pass.
  • Replaying the captured 2,249-request batch gives byte-identical lint output before and after.
  • Serial fact collection for that batch drops from 0.25 s to 0.21 s.

SQLCLEAN005 cloned every query that carries a WITH clause, including all of
its CTE bodies, only to drop the WITH and read the remaining table names.
Count table references in the whole query and subtract those inside the
WITH instead, so a name read both inside and outside still counts as used.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying sqlclean with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1fed2d1
Status: ✅  Deploy successful!
Preview URL: https://63133f4f.sqlclean.pages.dev
Branch Preview URL: https://perf-unused-cte-no-clone.sqlclean.pages.dev

View logs

@kvlonge
kvlonge merged commit c8ce21f into main Nov 2, 2026
15 checks passed
@kvlonge
kvlonge deleted the perf/unused-cte-no-clone branch November 2, 2026 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant