Repository navigation
Enable query_plan_join_shard_by_pk_ranges by default - #117856
Conversation
The setting was introduced in 25.4 (#74733) as an opt-in with the note that it "usually does not speed up queries but may lower memory consumption". That is true for the hash join it was written against, but two later default-on features made it a no-op there anyway on a stock server: join runtime filters put a `BuildRuntimeFilter` step on the build side, which the pass does not look through, and since 26.5 the default hash join is `SpillingHashJoin` (`max_bytes_ratio_before_external_join = 0.5`), whose `hasDelayedBlocks()` excludes it from sharding. Hash joins are sharded only when both are switched off. Where the setting does apply, `full_sorting_merge` and `parallel_full_sorting_merge` on a join key that is a primary-key prefix of both tables, it turns a single-threaded merge join into one streaming merge join per primary-key range, on all threads. Two 50M-row tables `ORDER BY id`, 16 threads, min of three interleaved runs: 50M x 50M inner join 1436 ms -> 222 ms, 583 MiB -> 69 MiB 50M x 1M dimension 561 ms -> 76 ms, 289 MiB -> 69 MiB ... with a filter matching 10% 186 ms -> 123 ms, 338 MiB -> 37 MiB ... with a filter on the right 605 ms -> 143 ms, 283 MiB -> 68 MiB 50M LEFT JOIN 1M 700 ms -> 121 ms, 315 MiB -> 68 MiB `parallel_full_sorting_merge` deliberately skips hash-sharding of already-sorted inputs (a read in order), so without this setting it also runs such joins single-threaded. For `hash`/`parallel_hash` with runtime filters and spilling disabled, the effect is neutral for `parallel_hash` (357 -> 368 ms on the first query, higher peak memory), a large win for single-threaded `hash` (2465 -> 387 ms), and 2x slower on the skewed filter, since the ranges are assigned to threads statically. The setting description now says so instead of the old blanket statement. The setting is randomized in the functional tests, so the suite already runs with it on 30% of the time; the randomization now favors the new default. `04105_limit_by_into_sort_explain` is a `no-random-settings` test that inspects the pipeline shape around a `full_sorting_merge` join, so it pins the setting off. Co-Authored-By: Claude Code <[email protected]>
|
Workflow [PR], commit [575440e] Summary: ✅
AI ReviewSummaryThis PR flips Final Verdict
LLVM Coverage ReportMeasured on commit 575440e.
Changed lines: Changed C/C++ lines covered: 21/21 (100.00%) · Uncovered code |
This comment was marked as low quality.
This comment was marked as low quality.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 29 s compile time in total, 8 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: Release-transition baseline: compared against the most recent available 26.9 masters at or before the trusted base-commit anchor because no 26.10 master baseline exists there. This PR turns on clickbenchFlagged queries (4 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (4 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
|
🕵 Thanks — both are now sequenced out, and the branch is merged with master (
I have added both to the |
|
🕵 CI status of the previous head
@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117856&sha=de4b57e08b87d3cc512f0c0fc20fcffb03393f2c&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 ( |
|
Investigated From the job's own
Three full
That is the function #116446 guards: when the So no separate PR is needed for this failure, but #116446 is waiting on a design call rather than on code: merge the guard as a stopgap, or implement bucket-aware shuffle support. I set out what each option costs on 2026-09-02 (#116446 (comment)). I will implement the proper support if that is preferred. |
The `Style check` job failed on `settings_changes_history`: the `query_plan_join_shard_by_pk_ranges` record sat in the `26.9` block, while the version in `cmake/autogenerated_versions.txt` has moved on to `26.10`. A default change made now has to be recorded under the current version block, otherwise `compatibility` attributes the flip to a release that shipped the old default. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Thanks @groeneai — #116446 merged on 2026-09-13 and is now in this branch (master merged in as Also fixed the |
…nges-default # Conflicts: # src/Core/SettingsChangesHistory.cpp
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117856&sha=5b450963619f82cb16e3177cd2d592857951370d&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 ( |
|
Investigated. This is the Cascades route of #115986, and the fix is already in progress in your own PR #119814 ("Keep the bucket-order promise of a shard aggregation in the Cascades optimizer"), still open. Attribution on this run. The abort is query Fix absence in the tested build. #119814 is unmerged, and Not caused by this PR. No new PR from me then; #119814 owns it. |
…nges-default Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Merged master ( The only red on @groeneai, investigate the failure: the link above, and provide a fix in a separate PR if |
|
The red at
That class no longer blocks. Since #122782 (merged 2026-09-29), a |
|
Both have fixes in progress:
|
…ough a final `DISTINCT` `optimizeJoinByShards` treated every `DistinctStep` as transparent. A final `DISTINCT` (unless it skips stream merging, which happens only for per-partition reads that this optimization already rejects) merges its input into a single stream, so that side of the join ends up with one stream while the other side is still split into primary-key ranges. `JoinStep` then disables the sharded path on the stream-count mismatch, and for a `YShaped` join (`full_sorting_merge`) the unsharded fallback requires one port per side. Now only a preliminary `DISTINCT` is transparent. For hash joins this changes nothing at runtime (the sharding was already dropped on the mismatch), so the `04655` test now expects no `Sharding` marker for a final `DISTINCT`, and shows sharding on the same tables without `DISTINCT` instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed On @groeneai, investigate the failure: the link above, and provide a fix in a separate PR. If a fix is already in progress, link it here. |
|
The slowdown is a property of the candidate binary, which is why it survives the restart. The machine code of No fix is in progress for this, and I don't have one. The only difference I can find is where the code lands, and it does not reproduce on my machine, so I can't narrow it down without a Sapphire Rapids host. These flips were more common before the kernel: |
|
🕵 Merged master ( Thanks @groeneai for the The only red on @groeneai, check whether #123841 also covers this occurrence, a replica in read-only mode during the drain. If it does not, provide a fix in a separate PR. The |
|
#123841 covers it. The read-only replica is a side effect of the same lock wait, not a ZooKeeper problem: This time the lock owner is I reproduced it on the official 26.9.11.2 server with the same chain (a killed mutation on a replicated table, plus that ON CLUSTER backup stuck in fault-injection retries over 80 replicated tables):
In 2 of the 3 passing runs the |
|
🕵 Thanks @groeneai — agreed, the |
|
All prerequisites are solved. |
|
The results for |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The code is trivial. LGTM.
Related: #112664
Related: #117786
Related: #118918
Related: #112402
Related: #112031
Related: #117873
Related: #111897
Prerequisites (all settled as of 2026-09-11). #112664 (the
NOT_FOUND_COLUMN_IN_BLOCKwhen aPREWHEREconsumes a sorting-key column that is not in the join output, issues #112402 and #112031) and #118918 (rows silently dropped when a Float primary key holds aNaN, issue #117873) are merged and in this branch, with their regression tests05099_join_shard_by_pk_prewhere_sorting_keyand05159_join_shard_by_pk_ranges_float_nan. The query condition cache poisoning of issue #111897 no longer reproduces on this branch (verified 3/3, withEXPLAINconfirming the read is still sharded);04065_join_shard_by_pk_with_qcccovers it. #117786 is not a prerequisite after all: thePartitionedFinishSortingabove a chained merge join, which itspending_sorting_steps/JoinPipelineType::YShapedguard fixes, only comes into existence because #117786 itself makes aFullSortingMergeJoinadvertise the order of its output — chains of merge joins with the sharding on give identical results here and never hitJoin is supported only for pipelines with one output port, which has no CIDB occurrence in the fleet since 2026-09-04.Why it was off
query_plan_join_shard_by_pk_rangeswas added in 25.4 (#74733) as an opt-in with the description "usually does not speed up queries but may lower memory consumption". That assessment was made for the hash join, and it still holds for it. But since then two default-on features made the optimization a no-op for hash joins on a stock server:enable_join_runtime_filters) put aBuildRuntimeFilterstep between the build-side read and the join, whichoptimizeJoinByShardsdoes not look through;max_bytes_ratio_before_external_join = 0.5) the default hash join isSpillingHashJoin, whosehasDelayedBlocks()is always true, and the pass excludes joins with delayed blocks.A hash join is sharded only when both are switched off. So on a default configuration the setting affects exactly
full_sorting_mergeandparallel_full_sorting_merge, and there it is the difference between a single-threaded merge join and one streaming merge join per primary-key range on every thread. (parallel_full_sorting_mergedeliberately does not hash-shard already sorted inputs such as a read in order, so it, too, runs single-threaded on such joins without this setting.)Measurements
Two 50M-row tables and a 1M-row table, all
ORDER BY id, 16 threads, min of three interleaved runs of on/off on a loaded box (system.query_log):full_sorting_mergeoff → onparallel_hash(spilling and runtime filters off) off → ona JOIN b USING id(50M × 50M)a JOIN dim USING id(50M × 1M)WHERE a.v < 5M(matches the first 10% of the key range)WHERE b.s = '5'a LEFT JOIN dim USING idparallel_full_sorting_mergegives the same numbers asfull_sorting_merge(the hash sharding is skipped for sorted inputs). Single-threadedhashgoes 2465 → 387 ms on the first query. The skewed filter is the known weak point of static range assignment: threads whose ranges have no matches finish early. That trade-off is now spelled out in the setting description, together with when the setting applies to hash joins at all.Testing
The setting is already randomized in the functional tests (30% on), so the suite has been exercising the sharded path for a long time; the randomization now leans towards the new default (70% on). Running all 1068
*join*stateless tests and the 45no-random-settingstests containing a JOIN with the setting forced on and off gives identical results, except04105_limit_by_into_sort_explain, ano-random-settingsEXPLAIN PIPELINEtest whosefull_sorting_mergejoin now pins the setting off (the test is aboutLIMIT BY, and #117786 rewrites that reference block anyway).The default change is recorded in
SettingsChangesHistory.cppforcompatibility.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
query_plan_join_shard_by_pk_rangesis enabled by default. Afull_sorting_mergeorparallel_full_sorting_mergeJOIN of two MergeTree tables whose join keys contain a prefix of the primary key of both tables now runs as independent streaming merge joins over primary-key ranges on all threads instead of a single-threaded merge join: several times faster and with several times less memory in our tests. Hash joins are affected only when both join runtime filters and spilling to disk are disabled. Setquery_plan_join_shard_by_pk_ranges = 0to restore the previous behavior.Workflow [PR]
Sync PR [sync-upstream/pr/117856]
Version info
26.10.1.2166-master(included in26.10and later)