Skip to content

Build the PREWHERE plan once per read pool instead of once per part - #111956

Open
raimannma wants to merge 10 commits into
ClickHouse:masterfrom
raimannma:hoist-prewhere-plan-out-of-per-part-loop
Open

raimannma wants to merge 10 commits into
ClickHouse:masterfrom
raimannma:hoist-prewhere-plan-out-of-per-part-loop

Conversation

@raimannma

@raimannma raimannma commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

getReadTaskColumns built the full PREWHERE execution plan via MergeTreeSelectProcessor::getPrewhereActions, and it is called once per part from MergeTreeReadPoolBase::buildReadTaskInfo. None of the inputs to that plan are part-specific, so every part rebuilt an identical plan — including tryBuildPrewhereSteps, which runs by default — and then discarded the compiled ExpressionActions, keeping only the per-step column name lists. Pipeline building is serial time on the query thread, so this lands directly in query latency on tables with many parts.

This builds the PrewhereExprInfo once in MergeTreeReadPoolBase and passes it to getReadTaskColumns. Work that is genuinely per-part stays per-part: required column injection, resolving columns against the part, and the adaptive granularity check. The resulting read task columns are unchanged.

Measured on a 710-part table with max_threads = 8, median QueryPipelineBuildMicroseconds: 0.90 ms → 0.65 ms for a 5-condition PREWHERE, and 2.06 ms → 1.42 ms for a 16-condition PREWHERE.

Changelog category (leave one):

  • Performance Improvement

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

Reduced query pipeline building time for MergeTree tables with many parts by constructing the PREWHERE execution plan once per query instead of once per part.


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

`getReadTaskColumns` built the full PREWHERE execution plan via
`MergeTreeSelectProcessor::getPrewhereActions` on every call, and it is called
once per part from `MergeTreeReadPoolBase::buildReadTaskInfo`. None of the
inputs to that plan are part-specific, so every part rebuilt an identical plan
- including `tryBuildPrewhereSteps`, which runs by default - and then threw the
compiled `ExpressionActions` away, keeping only the per-step column name lists.

Build the `PrewhereExprInfo` once in `MergeTreeReadPoolBase` and pass it to
`getReadTaskColumns`. Work that is genuinely per-part stays per-part: required
column injection, resolving columns against the part, and the adaptive
granularity check. The resulting read task columns are unchanged.

Measured on a 710-part table with `max_threads = 8`, median
`QueryPipelineBuildMicroseconds`: 0.90 ms -> 0.65 ms for a 5-condition
PREWHERE, and 2.06 ms -> 1.42 ms for a 16-condition PREWHERE.

The members `row_level_filter`, `prewhere_info` and `actions_settings` of
`MergeTreeReadPoolBase` are removed because building the plan in the
constructor leaves them without any reader.
@raimannma
raimannma force-pushed the hoist-prewhere-plan-out-of-per-part-loop branch from 5efb1a0 to fe2f5c6 Compare July 26, 2026 15:11
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [595a62d]

Summary: ✅


AI Review

Summary

The PR builds the PREWHERE plan once per read pool and reuses it when determining each part's read columns. Part-specific defaults, mutation steps, and granularity handling remain per part. The reported pipeline-building measurements support the performance improvement. No actionable findings remain at head 595a62d5b57e497a9bf09b52ba0e02bda9a87874; the earlier projection-index concern no longer applies to the current splitter interface.

Final Verdict

Status: ✅ Approve.

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

clickhouse-gh Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 46/46 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 595a62d5b with master f30d7ec4f (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

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

Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -129.6 s (-6%) in total before that adjustment.

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 4, 2026
raimannma and others added 2 commits October 2, 2026 01:30
# Conflicts:
#	src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp
…an-out-of-per-part-loop

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…an-out-of-per-part-loop

# Conflicts:
#	src/Storages/MergeTree/MergeTreeReadPoolBase.cpp
…an-out-of-per-part-loop

# Conflicts:
#	src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp

This branch has not been deployed

No deployments
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-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants