Skip to content

Use a compressible payload in the lazy_materialization_final_limit performance test - #123719

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:lazy-materialization-final-limit-small-granules
Oct 5, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:lazy-materialization-final-limit-small-granules

Conversation

@groeneai

@groeneai groeneai commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #122571 (comment)

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):

The lazy_materialization_final_limit performance test now uses a compressible payload at the default index granularity, so its LIMIT 100 query reads about 38 MB instead of about 285 MB.

Description

Performance Comparison (arm_release, master_head, 6/6) flagged #1 as a confirmed regression on #122571, which only changes another test: job, 31.3 -> 36.9 ms (+17.8%), all 7 right runs slower.

Since #122479 both servers read the same data (equal read counters in 1913 of 1913 arm runs; arm runs with #1 more than 10% apart fell from 21.3% to 2.4%). What remains is the read itself: only 47 rows pass FINAL WHERE v = 7, so LIMIT 100 is never reached and #1 lazily reads payload for those 47 rows from the default-size granules around each, about 285 MB of incompressible 2 KB strings per run. In the arm runs still more than 10% apart, the slow server spends 21% longer (median) in that page-cache read while bytes, rows and user CPU are equal.

The table keeps the default index granularity. Each 2000-byte value now repeats a random 200-byte string 10 times, so it compresses about 10x and the same read copies about 38 MB out of the page cache. The value width, the two intersecting parts, the queries and the plan (LazilyReadFromMergeTree) are unchanged; #1 with lazy materialization disabled is 3.8x slower (5.0x before).

Validated with a master release build on x86, two servers under perf.py --stop-merges: #1 reads 38.0 MB instead of 284.3 MB, identically on both servers, with 98 marks per part as before, and spends about 6 ms instead of 47 ms in page-cache reads. Timing #1 on two freshly filled tables in one process, 0 of 12 trials differed by more than 5% (3 of 12 before). The arm slow mode does not reproduce on x86, so the arm effect is inferred from the read volume.


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

Version info

  • Merged into: 26.10.1.1616-master (included in 26.10 and later)

…e test

Query ClickHouse#1 (`FINAL WHERE v = 7 LIMIT 100`) never reaches its limit: only 47 rows
survive FINAL, so it scans all of (k, v) and lazily reads `payload` for those
47 rows. With default granules (~5-6K rows of 2 KB random, incompressible
strings, ~10 MB each) every match decodes ~6 MB of payload, ~285 MB in total.

After ClickHouse#122479 both compared servers read identical data, but on arm one server
of a pair still copies those same bytes out of the page cache ~20% slower in a
few percent of fills, and with merges stopped all runs of the query carry it:
PR ClickHouse#122571 got a confirmed +17.8% on `Performance Comparison (arm_release,
master_head, 6/6)` this way.

With `index_granularity = 256` a granule is ~0.5 MB and one compressed block,
so each match reads ~0.5 MB. Measured on a two-server rig with the XML's own
statements: ClickHouse#1 ReadCompressedBytes 284.1 MB -> 31.8 MB, SelectedRows 1,015,104,
#0 and ClickHouse#2 read less too (58.7 -> 13.3 MB). The test still measures FINAL over
the same two intersecting parts with lazy materialization of the wide payload.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 3, 2026
@groeneai

groeneai commented Oct 3, 2026

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

Own cold review plus a second-model gate (codex) on 74e637c85582: no blocker or major findings.

Finding Verdict
💡 The XML comment's "reads little more than those rows" overstates: each match still reads its whole 256-row granule (about 0.5 MB for one 2 KB row). Noted, not blocking (wording only).

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? The deterministic part, yes: with the current fixture #1 reads 283.6-284.3 MB of compressed data for 47 result rows on every run and both servers (two-server perf.py --stop-merges rig, master release build). The arm timing slow mode itself does not reproduce on this x86 machine, so it is not claimed as reproduced.
b Root cause explained? Only 47 rows pass FINAL WHERE v = 7, so LIMIT 100 is never reached and the lazy read of payload decodes the default-size granules (5-6K rows of incompressible 2 KB strings) around every match, about 285 MB. In post-#122479 arm runs still more than 10% apart, the slow server spends 21% longer in that page-cache read with equal bytes, rows and user CPU.
c Fix matches root cause? Yes: index_granularity = 256 shrinks the exposed read (one ~0.5 MB block per match) instead of widening a tolerance, pinning threads or reducing the data.
d Test intent preserved / new tests added? Same queries, same two intersecting 500K-row parts, same filter, LIMIT and 2 KB payload. EXPLAIN still shows LazilyReadFromMergeTree / Lazily read columns: payload / FINAL: 1 / Parts: 2; #1 with lazy materialization off is 10.8x slower (4.5x before).
e Both directions demonstrated? Same rig and binary, base vs new: #1 ReadCompressedBytes 284.3 MB -> 31.8 MB, equal on both servers; two freshly filled tables timed alternately in one process, #1 beyond 5% in 2 of 10 trials before, 0 of 10 after.
f Fix is general across code paths? N/A (performance-test fixture, no code change). Other wide-payload FINAL tests were not flagged and are not touched.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for a fixture. The read cut holds at max_threads 8 (1/9.0) and 12 (1/9.0, CI arm), and both servers read identical bytes.
h Backward compatible? (maintainer-approved exception only) Test-only. release_base comparisons use the release's own test file until a release carries this.
i Invariants and contracts preserved? Each INSERT still writes exactly one part (the throwIf passes) and both servers get identical parts (1955 marks each).

Session id: cron:clickhouse-impl-slot-5:20261003-155500

@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5374f50]

Summary: ✅


AI Review

Summary

This PR keeps lazy_materialization_final_limit at the default granularity and makes the wide payload column compressible by repeating a 200-byte random fragment 10 times, which preserves the query shape while substantially shrinking the lazy read that was adding noise to the benchmark. I did not find blocker or major issues in the current diff.

Missing context / blind spots
  • ⚠️ I did not independently run the arm_release performance comparison locally because this checkout does not include a runnable ClickHouse build; the arm-specific effect is reviewed from the XML change, the implementation semantics of randomPrintableASCII, and the measurements recorded in the PR discussion.
Final Verdict

✅ No blocker or major findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Oct 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Oct 3, 2026
@PedroTadim PedroTadim self-assigned this Oct 5, 2026
CREATE TABLE lazy_mat_final_limit (k UInt64, v UInt64, payload String)
ENGINE = ReplacingMergeTree
ORDER BY k
SETTINGS index_granularity = 256

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@groeneai I prefer to test lazy_materialization_final_limit with default granularity, which is going to be the real production case. Can you increase the INSERT size or do something else instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in 5374f50: the table keeps the default granularity, and the payload is compressible instead: each 2000-byte value repeats a random 200-byte string 10 times (about 10x with LZ4). #1 still never reaches LIMIT 100 and lazily reads its 47 rows from default granules, but that read is now 38.0 MB instead of 284.3 MB (perf.py --stop-merges on two servers, 98 marks per part as before), and lazy materialization still makes #1 3.8x faster (5.0x before).

A larger INSERT alone makes it worse: with more matching rows #1 reaches LIMIT 100 and lazily reads 100 rows instead of 47, each costing about half a default granule (measured with 200-byte values: 116 MB at 1.2M rows per INSERT vs 50 MB at 500K).

…ation_final_limit

Keep the default index granularity, as production tables have, and shrink
the lazy read through the payload instead: each 2000-byte value now
repeats a random 200-byte string 10 times, so LZ4 compresses it about 10x.
The value width, the two intersecting 500K-row parts, the queries and the
lazy materialization plan stay as they were.

Query ClickHouse#1 never reaches its LIMIT 100 (47 rows match after FINAL) and
lazily reads the matching rows out of default-size granules, so most of
its time was copying ~284 MB of incompressible payload out of the page
cache, the part that differs between the two compared servers on arm.

perf.py --stop-merges on two servers, x86 master release build: ClickHouse#1 reads
38.0 MB instead of 284.3 MB, the same on both servers, with 98 marks per
part as before, and spends ~6 ms instead of ~47 ms in page-cache reads.
ClickHouse#1 with lazy materialization disabled is 3.8x slower (5.0x before).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Use small granules in the lazy_materialization_final_limit performance test Use a compressible payload in the lazy_materialization_final_limit performance test Oct 5, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 5, 2026
Merged via the queue into ClickHouse:master with commit 58967e0 Oct 5, 2026
149 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 5, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... 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-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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