Repository navigation
Add merge_use_batch_sorting_queue MergeTree setting for ordinary merges - #108468
alexey-milovidov merged 55 commits into
Conversation
|
Why closing? |
|
Hey I didn't see any activity from CH on this PR and figured that it might not be relevant or I was missing context, so I closed it as part of a cleanup of some of my PRs. Happy to work on it if it is of interest! |
|
We review 100% of PRs. |
|
Workflow [PR], commit [b5a3923] Summary: ✅
AI ReviewSummaryThis PR adds the opt-in Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 275/279 (98.57%) · Uncovered code |
|
📊 Cloud Performance Report 🟢 AI verdict: This PR adds a new default-off MergeTree setting (merge_use_batch_sorting_queue) that reduces per-row overhead during background merges; it does not touch the SELECT execution path, so it cannot plausibly change ClickBench or TPC-H query latencies. ClickBench Q17's -12.1% is downgraded to not-sure as variance. The larger improvements (ClickBench Q23 ×2.3 faster, TPC-H Q7 ×8.2 faster, Q8 ×2.2 faster) are deterministically locked by their large, consistent magnitude, but since the diff can't explain SELECT speedups these most likely reflect a baseline/era shift (small source sample of 6 runs) rather than the PR itself. Q32 is borderline (+5.3%) with mixed signals and is also off the changed path. clickbench🟢 1 improved · Flagged queries (3 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_official🟢 2 improved Flagged queries (2 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
|
The upgrade check compares against the 26.7 release, so record the new `MergeTree` setting in the 26.8 history section. CI: ClickHouse#108468
|
Add this MergeTree setting to randomization in CI. |
Add the new `MergeTree` setting to `MergeTreeSettingsRandomizer` in `tests/clickhouse-test`, so stateless tests exercise both the default and the batch sorting queue merge paths. The randomized value is injected into `CREATE ... MergeTree` queries only when the table does not already specify the setting (see `ClientBase::addMergeTreeSettings`), so the tests added by this pull request, which pin the setting explicitly to compare both paths, keep their own values.
Add the setting to `hot_table_settings` in `ci/jobs/buzzhouse_job.py`, so the fuzzer picks it for `MergeTree` tables more often and exercises the batch sorting queue merge path against randomly generated schemas.
|
The previous |
|
Hi @alexey-milovidov it looks like this run requires manual approval. Can you approve it? |
`MergingSortedBatchLimits.MaxBlockSizeBytesWithAggregateFunctionState` failed: the batch queue returned a 1-row block while the default queue returned 6 rows, and the expected row count was hardcoded. The two inputs were shaped so that the second one was consumable as a whole chunk, which makes the batch queue take the fast-forward path in `MergingSortedAlgorithm::mergeBatchImpl` - it flushes the rows accumulated so far and then inserts the source chunk directly. The default queue does not fast-forward there, because `SortCursorHelper::totallyLessOrEquals` compares against the first row of the other cursor, so the two queues legitimately disagree about block boundaries in that case. Interleave the two inputs instead, so neither of them is consumed as a whole chunk and the comparison actually exercises the byte limit predictor. Also replace the hardcoded row count with a non-vacuity check: the exact number of rows depends on `sizeOfData` of the aggregate state, which differs between build types.
`merge_use_batch_sorting_queue` was registered between `merge_max_block_size_bytes` and `merge_max_bytes_to_prewarm_cache`; move it to its alphabetical place in the list.
|
🕵 Approved the workflow run and pushed an update to this branch:
The AI review verdict on the previous head is stale: both of its minimum required actions (treating |
`merge_use_batch_sorting_queue` inserts runs of consecutive rows from the same source part with a single `insertRangeFrom`. But `MergedData::rowsToInsertBeforeFlush` returned 1 when any merged column had a non-additive `IColumn::byteSizeAt` (`LowCardinality`, `AggregateFunction`, dynamic columns), so with `merge_max_block_size_bytes` set (10 MiB by default) such merges fell back to single-row batches, which cost more than the default queue: enabling the setting made such merges slower. Remove that special case and let the existing per-row byte prediction cover these columns as an estimate. For `LowCardinality` the estimate is pessimistic (a repeated value adds only an index entry), so batches are cut no later than needed, and any remaining error is bounded by one batch because `hasEnoughRows` measures the actual size before the next one. Wide values still cut the batch where the row-by-row path does, including values inside columns like `Map(LowCardinality(String), String)`, so a run of huge values does not end up in a single granule. The special case was added during review only to reproduce the default queue's flush boundaries exactly (ClickHouse#108468 (comment)), which is not a correctness property: the two queues already disagree on boundaries through the whole-chunk fast path. With the setting enabled, a horizontal merge of a table with a `Map(LowCardinality(String), LowCardinality(String))` column (18M rows) is 21% faster than with the setting disabled (18% slower before this change), and on the ClickStack `otel_logs` schema horizontal merges are 12% faster and vertical merges no longer 11% slower. Related: ClickHouse#108468 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The `!global_ctx->projection` guard in `createMergedStream` kept every child `MergeTask` that merges projection parts (the projection-part merges of a parent merge and the temporary-part merges of `MergeProjectionPartsTask`) on the default sorting queue, even with `merge_use_batch_sorting_queue = 1`. It was added in ClickHouse#108468 for missing coverage, not for a defect. Normal projection merges are `Ordinary` and row-preserving; aggregate projections and children of DEDUPLICATE/CLEANUP parents still have `merge_may_reduce_rows` and keep the default queue. `_parent_part_offset` is remapped when the projection parts are read, not by the sorting algorithm. The new test compares the merged projection parts (rows and positions) between the two queues for a normal projection with another sorting key, a `_parent_part_offset` projection and a commit-order projection, in horizontal and vertical merges, asserts via part_log that the projections were merged rather than rebuilt, and covers the temporary parts that `MATERIALIZE PROJECTION` writes for a part larger than `min_insert_block_size_rows`. Related: ClickHouse#127826 Related: ClickHouse#108468 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #38022
Related: #38859
Adds
merge_use_batch_sorting_queue, a BooleanMergeTreesetting for ordinaryMergeTreemerges. It has no effect on merges that change rows, such as those performed byReplacingMergeTreeorAggregatingMergeTree.The setting defaults to
false, preserving the existing merge behavior. When set totrue, it uses the batch sorting queue in the ordinary merge sorting stage.The batch sorting queue is already used in several query sorting paths. This change makes it available for background merges as an opt-in table setting, so users can enable it for merge-heavy workloads where
MergingSortedTransformis a significant part of merge time.The PR also adds targeted stateless coverage for:
merge_use_batch_sorting_queue = falsevstruecorrectness across horizontal and vertical merges with a broad set of data types;Benchmark results from local runs with
merge_use_batch_sorting_queuedisabled and enabled:MergeTotalMillisecondschangeMergingSortedMillisecondschangeThe benefit is workload dependent. It is largest when sorted merging is a significant fraction of the total merge time, and smaller when writing, compression, or other merge work dominates.
This PR was developed with AI assistance from Codex and Claude.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add the
merge_use_batch_sorting_queueMergeTreesetting to optionally use the batch sorting queue for ordinaryMergeTreemerges, reducing CPU and wall-clock time for merge workloads where sorted merging is a significant cost.Version info
26.8.1.1424(included in26.8and later)