Skip to content

Estimate the read range of a compact part stream by stripes, not by columns - #131707

Merged
alexey-milovidov merged 7 commits into
masterfrom
compact-marks-estimate-per-stripe
Nov 20, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
compact-marks-estimate-per-stripe

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

MergeTreeReaderStreamAllOfMultipleColumns::estimateMarkRangeBytes estimates how much a compact part stream reads, to size its buffer and announce the read. It did this for every column of the part separately, at about a dozen getMark calls per column and mark. A compact part with substream marks (write_marks_for_substreams_in_compact_parts) has a column per substream, and a JSON column with many paths has thousands of them: 7025 for t_json_2 in the compact_part_subcolumns perf test.

So MergeTreeReaderStream::init cost the marks of the first task times the number of substreams. With one thread, SELECT data.a0 FROM t_json_2 spent 2.3 s of CPU there. Longer tasks make it worse. The stripe alignment of the columns cache gives longer tasks, so this query was 3x slower in the perf run of #123890, which enables the cache by default: https://performance.ci.clickhouse.com/runs/pr-123890-0d5d90b4d8401b30eb4ac3bd711d80f25f62b8f2

The stream reads all columns, and the columns of a stripe lie together in the file, so the estimate now walks whole stripes. A stripe ends where getStartOfNextStripeMark says it does, and the cost is O(marks). The sum is also accurate now. Each tiny substream used to count its shared compressed block again, so on the real marks of that table a 25 KB range was estimated at 175 MB. The maximum sizes the read buffer, and it is the same as before. The reader seeks to every column it reads, so the maximum is the largest span of a single column, not the whole stripe. As in getRightOffsetOneColumn, the span continues to the column's end in the next stripe when the column's mark there is in the middle of a compressed block. It is computed in the same walk, at one to three getMark calls per column and stripe. The new test 05768_compact_part_read_buffer_per_column checks that reading one of 100 separately compressed columns does not read the whole stripe.

SELECT data.a0 FROM t_json_2, median of 5 runs, master 2f2763156354 with only this change relinked:

case before after
max_threads = 1 2.34 s 0.90 s

Later, on a 10M-row local table, the same query with max_threads = 1 took 2.2 s on master and 0.92 s with the final version. The read buffer maximum was equal to master's for every stream checked, including the read_from_comp_parts table, the compact_part_subcolumns tables, and parts with Nullable, LowCardinality, Tuple and JSON columns.
| default threads | 0.216 s | 0.182 s |
| columns cache on, max_threads = 8 | 0.566 s | 0.278 s |

Compact-part, JSON and subcolumn stateless tests (724 passing) show no new failures against the unpatched binary. The existing perf test compact_part_subcolumns covers the speed.

Related: #123890

Changelog category (leave one):

  • Performance Improvement

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

Faster start of reading from compact parts with many substreams, such as parts with a JSON column with many paths.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.70-master (included in 26.12 and later)

…olumns

`MergeTreeReaderStreamAllOfMultipleColumns::estimateMarkRangeBytes` estimated the bytes to
read for every column of the part separately, at about a dozen `getMark` calls per column
and mark. A compact part with substream marks has a column per substream, and a `JSON` column
with many paths has thousands of them (7025 for `t_json_2` of the `compact_part_subcolumns`
perf test). So `MergeTreeReaderStream::init` cost the number of marks of the first task times
the number of substreams: with one thread, `SELECT data.a0 FROM t_json_2` spent 2.3 s of its
CPU there. Longer tasks make it worse, and the stripe alignment of the columns cache gives
longer tasks, which made that query 3x slower with the cache on.

The stream reads all columns, and the columns of a stripe lie together in the file, so the
estimate now walks whole stripes. A stripe ends where `getStartOfNextStripeMark` says it does.
This costs O(marks), and the sum is also accurate now: each tiny substream used to count its
shared compressed block again, so a range of 25 KB was estimated at 175 MB.

Measured with `SELECT data.a0 FROM t_json_2` (median of 5): 2.34 s -> 0.90 s with
`max_threads = 1`, 0.216 s -> 0.182 s with the default number of threads, and with the
columns cache on and `max_threads = 8`, 0.566 s -> 0.278 s.

https://performance.ci.clickhouse.com/runs/pr-123890-0d5d90b4d8401b30eb4ac3bd711d80f25f62b8f2
Related: #123890

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

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [37ec9d1]

Summary: ✅


AI Review

Summary

The PR reduces initialization work for compact parts with many substreams by estimating total bytes per stripe while sizing the read buffer per column. Reviewed head 37ec9d10bec93fd9658ff6a837dc6d9e20a33968, including the latest shared-compressed-block fix and all prior discussion. The previous buffer-sizing finding is addressed; no outstanding actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 37ec9d10b with master e627fea5e (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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 15, 2026
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review November 16, 2026 16:41
alexey-milovidov and others added 3 commits November 19, 2026 13:30
…lumn, not by a stripe

The maximum returned by `MergeTreeReaderStreamAllOfMultipleColumns::estimateMarkRangeBytes`
sizes the read buffer in `MergeTreeReaderStream::init`. The reader seeks to every column
it reads and skips the others, so the maximum must remain the largest span of a single
column in a stripe, as before. The sum still counts whole stripes, and the estimate
costs the number of stripes times the number of columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 20, 2026
alexey-milovidov and others added 2 commits November 20, 2026 03:54
… settings

The test read about 5 KB of a 35 KB part, so overheads that do not depend on
the read buffer size made it exceed the 50 KB threshold in CI: direct I/O
reads aligned 4 KB blocks for each ~50-byte column chunk, the page cache
reads whole blocks, and object storage coalesces remote reads on its own.

Use random values that do not compress, so a column chunk of a granule
takes 8 KB and a stripe of 100 columns takes 800 KB. Reading one column
now reads about 70 KB, and reading whole stripes would read 6.4 MB, so the
threshold of 1 MB leaves a wide margin on both sides. Disable the page
cache and parallel replicas for the measured query, and skip the test on
object storage, where the number of bytes read from a local file does not
reflect the read buffer.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131707&sha=latest&name_0=PR
PR: #131707

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

Copy link
Copy Markdown
Member Author

🕵 CI update (commits 510df1d08df3 and f1a4ccd2a6c9):

  • 05768_compact_part_read_buffer_per_column failed in the flaky check because the part was tiny: the query read about 5 KB of a 35 KB file. That let overheads unrelated to the read buffer size push it past the 50 KB threshold. Direct I/O reads a 4 KB aligned block for each ~50-byte column chunk, the page cache reads whole blocks, and object storage coalesces remote reads. The test now uses incompressible random values. Reading one column reads about 70 KB, and reading whole stripes would read 6.4 MB, against a 1 MB threshold. I replayed the randomized settings of all the failing CI runs locally (direct I/O, mmap, io_uring, page cache, columns cache) and every one passes. I also ran 20 runs with clickhouse-test --test-runs 20 and got no result mismatches.
  • 04161_unique_key_insert_duplicate is fixed on master by Pin 04161 and 05493 to query-based parallel replicas #132872, so I merged master into the branch.
  • read_from_comp_parts query Repair subtree2 #2 (ignore(c1, s1, c3), +13.4% on arm): I could not reproduce it. On aarch64 I built master's estimateMarkRangeBytes and this PR's version from the same tree and ran three alternating rounds of 60 queries on the perf test's table (50M rows, 43 compact parts). Master's version gave 6.81 / 6.77 / 6.78 QPS and this PR's gave 6.96 / 6.98 / 6.97, so this PR is about 3% faster. For the 6-column table both versions give the same per-column maximum and stripe sum, because each column chunk of a granule is its own stripe. I believe this result, like primary_key_analysis_wholly_matching_ranges #0, is noise.

`read_from_comp_parts` query #2 (`ignore(c1, s1, c3)`) was 14.6% slower on arm, confirmed after a server restart:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131707&sha=f1a4ccd2a6c93aa89b07ed36c163adb00a795587&name_0=PR&name_1=Performance%20Comparison%20(arm_release%2C%20master_head%2C%205%2F8)
PR: #131707

It was not noise. `ReadBufferFromFileDescriptorReadBytes` grew from 1.6 GB to 2.2 GB, and the number of reads from 12272 to 18171.
`estimateMarkRangeBytesOneColumn`, which was used before for every column, extends the span of a column to its end in the
next stripe when its mark there is in the middle of a compressed block (see `getRightOffsetOneColumn`). This happens
when substreams share a compressed block, like `String` and `Array` columns in that table. The stripe walk ended every
column in its own stripe. That gave a buffer of about 165 KB instead of about 585 KB, so the reader refilled the buffer
more often and read the skipped columns between the read ones again.

The walk now extends the span in the same way. It costs one more `getMark` per column and stripe, and one more stripe-end
search per stripe, only when some column continues into the next stripe. Locally, on a 10M-row copy of the perf table,
the maximum is now the same as master's for every stream: 80 streams of that table, `05768_compact_part_read_buffer_per_column`,
the `compact_part_subcolumns` tables, and parts with `Nullable`, `LowCardinality`, `Tuple`, `JSON`, several mark ranges and
`compress_per_column_in_compact_parts = 0`. The query reads 323 MB in 2495 reads, as on master (the PR read 440 MB in 3649).
`SELECT data.a0 FROM t_json_2 SETTINGS max_threads = 1` takes 0.92 s, against 2.2 s on master.

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

Copy link
Copy Markdown
Member Author

🕵 The read_from_comp_parts query #2 regression (ignore(c1, s1, c3), +14.6% on arm, confirmed after restart on f1a4ccd2) was real. It was not noise as I wrote before. ReadBufferFromFileDescriptorReadBytes grew from 1.6 GB to 2.2 GB, and the number of reads from 12272 to 18171.

Cause: in that table, substreams share compressed blocks. The String and Array marks have a nonzero offset_in_decompressed_block. Through getRightOffsetOneColumn, the old per-column estimate extended such a column to its end in the next stripe, which gave a read buffer of about 585 KB. The stripe walk ended every column in its own stripe, which gave about 165 KB. With the smaller buffer, the reader refilled more often and re-read the skipped columns between the ones it needed.

Fixed in 37ec9d1. The walk now extends the span in the same way, still at O(stripes × columns). I reproduced locally on a 10M-row copy of the table:

data-file bytes read reads
master 323 MB 2496
PR before the fix 440 MB 3649
PR now 323 MB 2495

With debug output, the maximum now equals master's for every stream I checked: the perf table (single-thread and filtered multi-range reads), the 05768_compact_part_read_buffer_per_column table, the compact_part_subcolumns tables, and parts with Nullable / LowCardinality / Tuple / JSON / compress_per_column_in_compact_parts = 0. SELECT data.a0 FROM t_json_2 SETTINGS max_threads = 1 takes 0.92 s, against 2.2 s on master (0.66 s with the smaller buffer).

The join_squashing_blocks #2 change on shard 4/8 does not read compact parts, so it is unrelated.

@stresshouse

stresshouse Bot commented Nov 20, 2026

Copy link
Copy Markdown

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 28 queries analysed

no significant changes detected. K_source=4 K_base=9 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 635babc2-c468-43c6-883a-9274e9e081f0
  • MIRAI run: afbc6c29-8955-43f3-90d6-98f0673b5ecd
  • PR check IDs:
    • clickbench_439085_1795166929
    • clickbench_439091_1795166929
    • tpch_adapted_1_official_439097_1795166929
    • tpch_adapted_1_official_439111_1795166929

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Nov 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 20, 2026
Merged via the queue into master with commit f42be40 Nov 20, 2026
195 checks passed
@alexey-milovidov
alexey-milovidov deleted the compact-marks-estimate-per-stripe branch November 20, 2026 16:50
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... pr-performance Pull request with some performance improvements 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.

2 participants