Repository navigation
Estimate the read range of a compact part stream by stripes, not by columns - #131707
Conversation
…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]>
|
Workflow [PR], commit [37ec9d1] Summary: ✅
AI ReviewSummaryThe 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 Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…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]>
… 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]>
|
🕵 CI update (commits
|
`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]>
|
🕵 The Cause: in that table, substreams share compressed blocks. The 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:
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 The |
|
📊 Cloud Performance Report ✅ AI verdict: 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
|
MergeTreeReaderStreamAllOfMultipleColumns::estimateMarkRangeBytesestimates 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 dozengetMarkcalls per column and mark. A compact part with substream marks (write_marks_for_substreams_in_compact_parts) has a column per substream, and aJSONcolumn with many paths has thousands of them: 7025 fort_json_2in thecompact_part_subcolumnsperf test.So
MergeTreeReaderStream::initcost the marks of the first task times the number of substreams. With one thread,SELECT data.a0 FROM t_json_2spent 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-0d5d90b4d8401b30eb4ac3bd711d80f25f62b8f2The 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
getStartOfNextStripeMarksays 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 ingetRightOffsetOneColumn, 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 threegetMarkcalls per column and stripe. The new test05768_compact_part_read_buffer_per_columnchecks 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, master2f2763156354with only this change relinked:max_threads = 1Later, on a 10M-row local table, the same query with
max_threads = 1took 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 theread_from_comp_partstable, thecompact_part_subcolumnstables, and parts withNullable,LowCardinality,TupleandJSONcolumns.| default threads | 0.216 s | 0.182 s |
| columns cache on,
max_threads = 8| 0.566 s | 0.278 s |Compact-part,
JSONand subcolumn stateless tests (724 passing) show no new failures against the unpatched binary. The existing perf testcompact_part_subcolumnscovers the speed.Related: #123890
Changelog category (leave one):
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
JSONcolumn with many paths.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131707]
Version info
26.12.1.70-master(included in26.12and later)