You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LIMIT BY on a stream already sorted by its keys is about 2x slower (26.6 -> 26.8) #123700
comp-query-executionRuntime execution pipeline, processors, vectorized operators, resource management during execution.performancereplay-findingRegression found by chReplay production query replay
ORDER BY k1, k2, ver DESC LIMIT 1 BY k1, k2 over a ReplacingMergeTree with String keys is about 2x slower on 26.8 than on 26.6, and about 4.8x slower on a single merged part. The single-threaded LimitBySortedStreamTransform spends about 4x more time on the same input rows.
Regression between 26.6.1.2301 (OLD) and 26.8.1.390 (NEW).
How to reproduce
Run the setup once, then measure only the SELECT on each version.
CREATETABLEdefault.t1 (c1 String, c2 String, c5 UInt64) ENGINE = SharedReplacingMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}', c5) ORDER BY (c1, c2);
INSERT INTOdefault.t1SELECT concat('UC', substring(hex(sipHash64(number % 25000)), 1, 22)) AS c1, substring(hex(sipHash64(number, 1)), 1, 11) AS c2, 1AS c5 FROM numbers(12000000);
INSERT INTOdefault.t1SELECT concat('UC', substring(hex(sipHash64(number % 25000)), 1, 22)) AS c1, substring(hex(sipHash64(number, 1)), 1, 11) AS c2, 2AS c5 FROM numbers(8000000);
OPTIMIZE TABLE default.t1 FINAL;
SELECTcount() FROM (SELECT c1, c2, c5 FROMdefault.t1ORDER BY c1 ASC, c2 ASC, c5 DESCLIMIT1 BY c1, c2)
SETTINGS compatibility ='25.8', max_threads ='8';
Performance
Original query (milliseconds; SQL omitted):
OLD 26.6.1.2301
NEW 26.8.1.390
NEW/OLD
12078
22657
1.88×
Synthetic repro above (milliseconds):
Query
OLD 26.6.1.2301
NEW 26.8.1.390
NEW/OLD
Query
439
2137
4.87×
Expected behavior
The LIMIT BY step on a stream already sorted by the LIMIT BY keys should cost about the same per row as in 26.6. The result is unchanged (identical counts on both versions).
Acknowledged. I'm investigating the LimitBySortedStreamTransform regression on sorted String keys; I'll follow up here with a PR (or my findings) shortly.
Fix: #123808. Since #106502, each group's search restarted on the leading key column, so on long leading-key runs it rescanned the rest of that run for every group (18.9 String comparisons per row here, against 2 before). The fix remembers the run of each key prefix within a chunk, and the issue query goes from 1.08 s to 0.31 s with identical results; negative LIMIT BY and the full_sorting_merge join had the same cost and are fixed too.
comp-query-executionRuntime execution pipeline, processors, vectorized operators, resource management during execution.performancereplay-findingRegression found by chReplay production query replay
Activity
fm4v commentedon Oct 3, 2026
@groeneai Please investigate the root cause of this regression and create a fix.
groeneai commentedon Oct 3, 2026
Acknowledged. I'm investigating the
LimitBySortedStreamTransformregression on sortedStringkeys; I'll follow up here with a PR (or my findings) shortly.groeneai commentedon Oct 4, 2026
Fix: #123808. Since #106502, each group's search restarted on the leading key column, so on long leading-key runs it rescanned the rest of that run for every group (18.9
Stringcomparisons per row here, against 2 before). The fix remembers the run of each key prefix within a chunk, and the issue query goes from 1.08 s to 0.31 s with identical results; negativeLIMIT BYand thefull_sorting_mergejoin had the same cost and are fixed too.Search each key column again only when the walk leaves its prefix's run
Track SortedKeyRuns memory and add perf arms for two-column sorted keys
Backport #123808 to 26.8: resolve conflicts
Backport #123808 to 26.9: resolve conflicts