Skip to content

Run two kway_merge_expensive_comparator sorts on a quarter of the rows - #131660

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/perf-kway-merge-expensive-comparator-shorter
Nov 15, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/perf-kway-merge-expensive-comparator-shorter

Conversation

@groeneai

@groeneai groeneai commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129258

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Description

From the #129258 audit: four full sorts of kway_merge_expensive_comparator.xml take more than 1 s on MasterCI since 2026-11-10. This PR shortens two of them: the collation sort (query 2, 5M rows: 1533 ms amd, 1202 ms arm) and the single-column string sort (query 3, 20M rows: 1932 / 1916 ms).

No existing query number or text changes: 2 queries are disabled and 2 are appended. Queries 2 and 3 get disabled="1" and are appended as 9 and 10 with numbers_mt(N / 4) and SETTINGS max_block_size = 16384, a quarter of the default 65409. numbers_mt returns blocks of exactly max_block_size rows, so each copy has as many blocks as its original (77 and 306): every per-stream merge keeps its number of cursors and the final merge stays 16-way. EXPLAIN PIPELINE of each original and its copy is identical.

Measured with perf.py on official MasterCI release builds (25 runs per pass): each copy takes 0.25 of its original's time, so about 380 / 487 ms on amd and 298 / 483 ms on arm, and about 32 s (amd) and 29 s (arm) less query time per master perf job. On the builds before and after #115869 (sorted array of cursors), query 2 is 1.161x faster and its copy 1.159x; before and after #126829 (cheaper collation), 2.179x and 2.111x. Query 3 and its copy show no change on either pair.

Queries 0 and 1 stay as they are: locally #115869 did not show on them beyond the A/A noise, so I have no measurement that shorter copies would keep their sensitivity.

Queries 9 and 10 start a new history (CIDB has only 0-8 for this test). 9 queries stay enabled, so every master job still runs all of them.

🤖 Generated with Claude Code


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

Version info

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

The collation sort (ClickHouse#2, 5M rows) and the single-column string sort (ClickHouse#3,
20M rows) take 1.2-1.9 s on MasterCI on both arches. They get
disabled="1" (text and number unchanged) and are appended as ClickHouse#9 and ClickHouse#10
on a quarter of the rows with max_block_size = 16384, a quarter of the
default 65409. `numbers_mt` hands out blocks of exactly max_block_size
rows, so each copy has as many blocks as its original (77 and 306): the
same number of cursors in every per-stream merge and the same 16-stream
final merge, with a quarter of the rows per block. EXPLAIN PIPELINE of
each original and its copy is identical (16 streams).

Measured on official MasterCI release builds, two servers pinned to 24
cores of one NUMA node, perf.py, 25 runs: copy / original time 0.248
(ClickHouse#2) and 0.252 (ClickHouse#3), projected CI medians 380 / 487 ms amd, 298 / 483 ms
arm. On the builds before/after ClickHouse#115869 (sorted array of cursors) ClickHouse#2 is
1.161x faster and its copy 1.159x; on those before/after ClickHouse#126829
(cheaper collation) 2.179x and 2.111x; ClickHouse#3 and its copy show no change
on either pair (1.005 / 1.008, 1.004 / 1.001), as the heap guard should.

#0 and ClickHouse#1 stay as they are: on this rig the ClickHouse#115869 change did not show
on them beyond the A/A resolution, so there was no measured guarantee
that copies would keep their sensitivity.

About 32 s (amd) and 29 s (arm) of query time less per master
performance job; 9 queries stay enabled, so all of them still run.

Related: ClickHouse#129258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The appended comment says "The first sorts of this file", but only queries 2 and 3 are copied AGREE, noted Comment-only: no query, setting or query number is affected

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261114-161900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? The cost is measured, not intermittent: MasterCI medians since 2026-11-10 of query 2 1533 / 1202 ms and query 3 1932 / 1916 ms (amd / arm), and 1157-1210 / 1570-1580 ms locally on the same official build.
b Root cause explained? Both queries are numbers_mt(N) ORDER BY ... FORMAT Null: the time is sorting N rows, linear in N at a fixed block count (copy / original 0.248 and 0.252 in every pass).
c Fix matches root cause? A quarter of the rows in blocks of a quarter of the size: the same number of blocks, so every merge keeps its number of cursors. The originals stay in the file, disabled.
d Test intent preserved / new tests added? EXPLAIN PIPELINE of each original and its copy is identical (16 streams). Before/after #115869, query 2 and its copy are 1.161x / 1.159x faster; before/after #126829, 2.179x / 2.111x; query 3 and its copy show no change on either, as the heap guard should. No new test: the file is the test.
e Both directions demonstrated? Each build pair ran twice, with the older build on the left and then on the right; the effect is the mean of both.
f Fix is general across code paths? N/A (test-only). Queries 0 and 1 are kept: neither pair showed on the originals beyond the A/A noise, so there was nothing to check copies against. Query 4 is below 1 s.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only).
h Backward compatible? (maintainer-approved exception only) No existing query number or text changes; the copies get the new numbers 9 and 10 (CIDB has only 0-8 for this test).
i Invariants and contracts preserved? 9 queries stay enabled (<= 10, all run); the create, fill and drop queries are unchanged; merges stay on as before (the enabled INSERTs); no query's time grows run over run in the final file.

Session id: cron:clickhouse-impl-slot-4:20261114-141505

@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [80d078c]

Summary: ✅


AI Review

Summary

Reviewed #131660 at 80d078ce4c99415c5b468133593b88645e861c7b, including all prior discussion and the current code. The PR replaces two expensive sort benchmarks with smaller copies while retaining the existing query indices. The proportional block-size reduction preserves the total block counts and comparator paths. The author's reported 25-run comparisons provide runtime and sensitivity evidence; these measurements were not independently rerun. No actionable findings.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-testing Test content only (no production code). labels Nov 14, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 15, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 15, 2026
Merged via the queue into ClickHouse:master with commit e7c09ad Nov 15, 2026
570 of 572 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 15, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. and removed comp-testing Test content only (no production code). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants