Skip to content

Optimize TopK through JOIN and ARRAY JOIN - #128926

Merged
nihalzp merged 3 commits into
masterfrom
yarik/top-k-through-array-join-dynamic-filter
Nov 4, 2026
Merged

nihalzp merged 3 commits into
masterfrom
yarik/top-k-through-array-join-dynamic-filter

Conversation

@yariks5s

@yariks5s yariks5s commented Nov 3, 2026 •

Copy link
Copy Markdown
Member
Query Before After
5M rows, 4-table LEFT JOIN chain, ORDER BY l.k DESC LIMIT 10 646 MiB, 0.068 s 1 MiB, 0.015 s
8192 rows of 4096 elements, ARRAY JOIN arr ORDER BY k LIMIT 1000 160 MiB, 0.070 s 64 MiB, 0.047 s
2M rows, ARRAY JOIN arr ORDER BY k DESC LIMIT 10 3 MiB, 0.008 s 3 MiB, 0.005 s

Related: #116550
Related: #115157

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The sort that query_plan_top_k_through_array_join and query_plan_top_k_through_join move now uses the top-K dynamic filter. This lowers memory for ORDER BY ... LIMIT over ARRAY JOIN with long arrays, and the JOIN optimization now goes through all joins of a LEFT JOIN chain.

The most notable optimizations are for regular JOINs:
image


Workflow [PR]
Sync PR [sync-upstream/pr/128926]

Version info

  • Merged into: 26.11.1.1330-master (included in 26.11 and later)

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6a6aff2]

Summary: ✅


AI Review

Summary

The PR enables dynamic top-K filtering for sorting moved below ARRAY JOIN and revisits inserted limits deeply enough to optimize chains of LEFT JOIN. No actionable findings. Both new stateless tests passed against the PR's Fast Test binary with max_threads = 1, max_block_size = 64, and use_query_condition_cache = 0, supplementing the successful Fast Test runs with small-block execution.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 3, 2026
@yariks5s yariks5s changed the title Omptimize memory cost for TopK through ARRAY JOIN Omptimize memory cost for TopK through JOIN and ARRAY JOIN Nov 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6a6aff299 with master ca06fcaaf (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

9 translation units recompiled, 37 s compile time in total, 9 of them have a recent master baseline.

Job report

@yariks5s yariks5s changed the title Omptimize memory cost for TopK through JOIN and ARRAY JOIN Omptimize TopK through JOIN and ARRAY JOIN Nov 3, 2026
@nihalzp nihalzp self-assigned this Nov 4, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 4, 2026
Merged via the queue into master with commit 1bbe8b6 Nov 4, 2026
185 checks passed
@nihalzp
nihalzp deleted the yarik/top-k-through-array-join-dynamic-filter branch November 4, 2026 13:49
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 4, 2026
@devcrafter devcrafter changed the title Omptimize TopK through JOIN and ARRAY JOIN Optimize TopK through JOIN and ARRAY JOIN Nov 6, 2026
yariks5s added a commit that referenced this pull request Nov 11, 2026
With the re-optimization depth from #128926 the notEmpty filter of the first
move goes below the next ARRAY JOIN, so the sort of an inner chain moves again.
The case showed the sort between the two ARRAY JOIN steps. Now it shows both
above one limited sort with the filter notEmpty(arr2) AND notEmpty(arr).
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 24, 2026
yariks5s added a commit that referenced this pull request Nov 24, 2026
Includes the backports of #116550, #128015 and #128926, which this change needs and which are not in 26.10 yet.
robot-ch-test-poll2 added a commit that referenced this pull request Nov 24, 2026
Cherry pick #128926 to 26.10: Optimize TopK through JOIN and ARRAY JOIN
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants