perf(schema-compiler): cache back-alias members per query#11322
Open
hank-sq wants to merge 3 commits into
Open
perf(schema-compiler): cache back-alias members per query#11322hank-sq wants to merge 3 commits into
hank-sq wants to merge 3 commits into
Conversation
Avoid repeating an expensive member traversal for every FILTER_PARAMS expansion while keeping intermediate planning phases uncached. Signed-off-by: Hank Ditton <hank@squareup.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Ensure nested alias gathering still recomputes phase-local results after the completed-query cache has been populated. Signed-off-by: Hank Ditton <hank@squareup.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Keep memoization internal so exported callers still receive fresh mutable maps, and verify full SQL and parameter parity with non-empty aliases. Signed-off-by: Hank Ditton <hank@squareup.com> Co-authored-by: Cursor <cursoragent@cursor.com>
hank-sq
marked this pull request as ready for review
July 23, 2026 23:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Check List
Description of Changes Made
FILTER_PARAMSexpansion can callallBackAliasMembersExceptSegments()once per filter expression. Each call currently traverses every member in the query, so schemas with many filter parameters and wide queries repeat the same expensive work hundreds of times during SQL generation.This change memoizes the completed alias map for each
BaseQueryinstance through an internalWeakMapused byFILTER_PARAMSandFILTER_GROUPexpansion. It deliberately bypasses the cache while the join graph is still being built and during nested alias-gathering traversals, then freezes the successfully computed internal result before reuse. The exportedallBackAliasMembersExceptSegments()method retains its existing behavior of returning a fresh, mutable map.The cache is query-instance scoped: no value is shared through
compilerCache,queryCache, users, security contexts, or requests. It becomes collectible with its owningBaseQuery.Correctness coverage
The new regression suite verifies:
BaseQueryinstances, even with shared compilers/query cachesFILTER_PARAMSexpansions perform 50 lookups but only one expensive alias traversalPerformance reproduction
A generated minimal schema with 100 dimensions and 500
FILTER_PARAMSexpansions was tested both directly throughPostgresQueryand end-to-end through a source-built local Cube server's/cubejs-api/v1/sqlendpoint. E2E requests varied filter values to avoid whole-query cache hits./sqlmedian: 587.45 ms → 65.79 ms (8.9× faster)A joined-schema benchmark with 10
FILTER_PARAMSper cube shows the optimization scaling beyond the synthetic single-cube case:Small single-cube queries showed no meaningful regression: queries with 0–1
FILTER_PARAMSadded only 0.3–1.1 µs (under 0.3%). All cached/uncached comparisons produced identical SQL or endpoint responses.Separately, we have been running a patched build with this change against our production workload and have observed substantial query-planning improvements:
This is an atypical,
FILTER_PARAMS-heavy setup with more than 100 filter parameters on each cube, so these measurements are workload-specific rather than representative of every Cube deployment.LTS backport request
We believe this change should be included in the Cube 1.6 LTS line (
lts/v1.6). It addresses severe query-planning latency inFILTER_PARAMS-heavy schemas while preserving generated SQL and parameter behavior, and the production measurements above show a material impact.Validation
yarn tscyarn lintinpackages/cubejs-schema-compiler(passes; existing warnings only)base-queryandjoin-hints-cache-pollutionregression suiteserror-reporter.test.tsMade with Cursor