Skip to content

Estimate the join inputs of the automatic parallel replicas probe from the single-node analysis - #130516

Merged
nickitat merged 15 commits into
masterfrom
fix-double-index-analysis-again
Nov 19, 2026
Merged

nickitat merged 15 commits into
masterfrom
fix-double-index-analysis-again

Conversation

@nickitat

@nickitat nickitat commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Automatic parallel replicas no longer repeats the index analysis of a join's tables when it builds the plan with parallel replicas to decide whether to use them.


Related: #129339
Related: #125687

Automatic parallel replicas builds and optimizes a probe plan with query_plan_optimize_primary_key off: if the probe plan is chosen, its reads take the index analysis of the single-node plan. But join optimization estimates every join input with estimateReadRowsCount, and for a read without an analysis that ran a full index analysis. Every read of the probe plan has none, so each probe of a query with a join analyzed the join's reads a second time. The analysis also pruned nothing, since the probe's reads have no index conditions, so the probe sized its joins from whole-table row counts.

Now estimateReadRowsCount takes find_single_node_read from join optimization (optimizeJoinLegacy, optimizeJoinLogical, and the IEJoin statistics in buildPhysicalJoinImpl). A read of the probe plan is estimated from an analysis its single-node read already has, and is never analyzed for the estimate:

  • the single-node read's own analysis, if it has one;
  • otherwise the result of selectRangesToReadForEstimation, which the single-node join estimate uses under a throwing read limit. It must not become the read's analysis, so ReadFromMergeTree now also keeps it in a separate field, returned only by getAnalyzedResultForEstimation. The probe then estimates from the numbers the single-node plan chose its join order by, and makes the same choice.
  • without either, or when the read cannot be paired, there is no estimate. A read that cannot be paired cannot take an analysis in transplantAnalysisToAllReads either, so such a probe is rejected anyway.

The new test 05508 compares index analysis rounds and marks checked against the primary key with and without automatic parallel replicas, for a join, tables written in an order the probe numbers differently, a table read twice, a CTE used twice, a CTE over a join used twice, and a throwing read limit. The probe used to add one round per read of a join and check every mark again; now it adds nothing. It also covers a join the single-node plan swaps under a throwing read limit without column statistics: with no estimate, the probe would not swap it and the plans would not match.


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

Version info

  • Merged into: 26.11.1.2892-master (included in 26.11 and later)
  • Backported to: 26.10.9.10

The probe plan of automatic parallel replicas is optimized without
`query_plan_optimize_primary_key`, so its reads have no index conditions and
no analysis of their own. Join optimization estimates every join input with
`estimateReadRowsCount`, which analyzed such a read on the spot: a second
index analysis of every read under a join, and an unpruned one, so the probe
also sized its joins from whole-table row counts.

Pass `find_single_node_read` to `estimateReadRowsCount` from join
optimization (`optimizeJoinLegacy`, `optimizeJoinLogical`, and the IEJoin
statistics in `buildPhysicalJoinImpl`). A read without an analysis is
estimated from the analysis of its single-node read, which is the one the
probe's reads take if that plan is chosen. Nothing is stored on the probe's
read.

In the new test, the probe of a two-table join used to add one round of
index analysis per table and check every mark against the primary key
again; it now adds none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Nov 10, 2026
@nickitat
nickitat added this pull request to stack #130792 November 11, 2026 13:07
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [869709a]

Summary: ✅


AI Review

Summary

Reviewed #130516 at 869709aaca42353736e21f5aa22ee4cd4bccc32e. The change avoids repeated index analysis when automatic parallel replicas estimates join inputs, keeping estimation-only results separate from execution analysis. No actionable findings. The focused regression passes repeatedly in current-head CI, and the before/after measurements recorded in the PR support the performance improvement.

Final Verdict

Status: ✅ Approve

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

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 869709aac with master 30ff53fe8 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 1d9af504a; 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

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

Job report

nickitat and others added 7 commits November 12, 2026 12:37
The previous commit took the estimate of a probe read from the read that
`find_single_node_read` pairs it with. That pairing goes by the name the
analyzer gave the table expression, and the two plans do not agree on these
names: the replicas plan's local plan is built from the tree
`buildQueryTreeForShard` returns, which renumbers `__tableN`. With a
three-table join written big table last, or a table read twice, the
pairing finds nothing and the estimate analyzed the probe's read in full,
unpruned, as before.

An estimate does not need to know which read is which. The probe now also
gets `find_single_node_reads_of_table`, every read of the single-node plan
of the same table, and `estimateReadRowsCount` takes the largest of their
analyses: the analysis itself when the table is read once or several times
alike (a CTE used twice), and an upper bound when its reads are filtered
differently. `find_single_node_read` stays as it is for direct read from a
text index, which needs the very read.

`makeDistributed`, `RuntimeFilterExchangeWiring` and the Cascades statistics
also call the estimate, but only under `make_distributed_plan`, which
rejects automatic parallel replicas, so they never see a probe plan.

`05508` covers the two shapes, and a CTE used twice; with the previous
commit the first two still added rounds of index analysis.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This is the shape of the B28 residual queries (q39-like: a CTE over a
four-table join, read twice with a different filter on each copy). The
plan with parallel replicas numbers the table expressions of each copy
from 1, so both copies of a table share one name there and have two
different names in the single-node plan. Pairing by name then finds
nothing, and with only the first fix the probe doubled the index
analysis (8 rounds against 16 locally, as in the replays).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A read of the probe plan of automatic parallel replicas now takes the
analysis of the single-node read that `find_single_node_read` pairs it
with, if that read already has one, and is left without an estimate
otherwise. It is never analyzed for the estimate: not the probe's own read,
which would prune nothing, and not the single-node one.

This replaces the previous commit's grouping of single-node reads by table,
which took the largest of their analyses when a read could not be paired.
A read that cannot be paired cannot take an analysis in
`transplantAnalysisToAllReads` either, so that estimate only helped a probe
the transplant rejects afterwards anyway.

Under a throwing read limit the single-node join estimates use
`selectRangesToReadForEstimation`, which keeps no result, so the probe finds
none to take. It used to analyze its reads then (6 index analysis rounds
against 4 without automatic parallel replicas in the new `05508` case); now
it adds none. In every `05508` case the outcome of the optimization is the
same as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n `05508`

With a throwing `max_rows_to_read`, the single-node join estimates use
`selectRangesToReadForEstimation`, which keeps no result, so the probe of
automatic parallel replicas finds no analysis to estimate from. Without
column statistics (`use_statistics = 0`) its join inputs then have no row
estimate at all. The single-node plan swaps a join whose small table is
written first, the probe does not, and the two plans cannot be matched:
`AutoParallelReplicasPlansNotComparable` is 1 and no statistics are
collected.

The case fails on this branch. Before the previous commit it matched, but
the probe analyzed its reads again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Under a throwing read limit, a single-node join estimate analyzes its read
with `selectRangesToReadForEstimation`, which must not become the read's
analysis, so the probe plan of automatic parallel replicas found none to
estimate from. Without column statistics its join inputs then had no row
estimate, it did not swap the join the single-node plan swapped, and the
plans could not be matched.

Keep that result on the read as well, in a field of its own that only
`getAnalyzedResultForEstimation` returns, and let the probe estimate from it
when the paired single-node read has no analysis. The probe now estimates
from the numbers the single-node plan chose by, without analyzing anything
itself; the executed read still analyzes again as before.

`05508_throwing_limit_swap` passes: the plans match, and the probe adds no
index analysis.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…alysis-again

# Conflicts:
#	src/Processors/QueryPlan/ReadFromMergeTree.cpp
…lysis

The row store for `String` keys (#125979),
merged from master, estimates the right input of a hash join for the average
bytes of its columns with `estimateReadRowsCount`, without
`find_single_node_read`. In the probe plan of automatic parallel replicas that
analyzed the read again, which `05508_throwing_limit_swap` caught after the
merge: 6 index analysis rounds against 5 without automatic parallel replicas.
Pass `find_single_node_read` there as well, like the other join estimates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Nov 12, 2026
@clickhouse-gh
clickhouse-gh Bot requested review from leshikus and maxknv November 12, 2026 17:39
The comment still described the first version, where a read without an
analysis was estimated from the single-node read however it got one. Now
the estimate only takes an analysis that read already has, or its last
estimation-only one, and otherwise gives none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Base automatically changed from autopr-text-index-direct-read to master November 14, 2026 14:01
@nickitat nickitat removed the submodule changed At least one submodule changed in this PR. label Nov 14, 2026
@nickitat
nickitat removed this pull request from stack #130792 November 14, 2026 23:44
@nickitat
nickitat changed the base branch from master to autopr-nested-queries-stacked November 14, 2026 23:44
@nickitat
nickitat added this pull request to stack #125688 November 14, 2026 23:44
@nickitat
nickitat marked this pull request as ready for review November 17, 2026 13:56
@nickitat
nickitat requested a review from devcrafter November 17, 2026 13:56
Base automatically changed from autopr-nested-queries-stacked to master November 17, 2026 14:49
@devcrafter devcrafter self-assigned this Nov 17, 2026
@devcrafter
devcrafter requested a review from groeneai November 17, 2026 20:44

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I built cd9539f (debug) and the same tree with the six src/ files reverted to the merge base, and ran both against a local 3-replica parallel_replicas cluster with the CI users.d/limits.yaml profile.

  • 05508 matches the reference on the PR head. With src/ reverted, every case reports 0 0 in the two same_* columns: the probe checks the marks against the primary key once more (join: 4 rounds and 248 marks against 2 and 124; throwing_limit_swap: 8 and 374 against 5 and 249).
  • Dropping only the getAnalyzedResultForEstimation() fallback changes just the last row (not_comparable 0 -> 1), so the swap case covers that path.
  • Mode 1 after a mode-2 run, with the cost-model settings of 05507: the same queries are adopted as before the change (the join, the table read twice, the join under a throwing limit; the big-table-last and swap shapes are evaluated and not adopted on both builds), the results equal those with enable_parallel_replicas = 0, and the index analysis rounds now equal the single-node run's in every case (2 or 3 more before).
  • getAnalyzedResultForEstimation() has a single reader, and the transplant still hands over getOrCreateAnalyzedResult(). The callers that do not pass find_single_node_read (makeDistributed.cpp, RuntimeFilterExchangeWiring.cpp, Cascades/Statistics.cpp) run only under make_distributed_plan, which QueryPlanOptimizationSettings rejects together with parallel replicas.

One nit: two comments in 05508 still describe the behaviour before a5723c3. Lines 16-17 ("there is no analysis for the probe to take") and 79-80 ("the probe finds none to take, and its reads are left without an estimate") no longer hold: under a throwing limit the probe now estimates from the single-node read's selectRangesToReadForEstimation result, as the second point shows.

@nickitat
nickitat enabled stacked auto-merge November 18, 2026 21:14
…alysis-again

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.cpp
#	src/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.h
#	src/Processors/QueryPlan/ReadFromMergeTree.cpp
@nickitat
nickitat added this pull request to the merge queue Nov 19, 2026
Merged via the queue into master with commit bc54914 Nov 19, 2026
386 of 387 checks passed
@nickitat
nickitat deleted the fix-double-index-analysis-again branch November 19, 2026 21:46
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 19, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 19, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 20, 2026
Since ClickHouse#130516, join optimization passes `find_single_node_read` to
`estimateReadRowsCount`, so a read of the probe plan of automatic parallel
replicas takes the analysis of its single-node read instead of being analyzed
again. The key path walk of the JOIN runtime filter guard estimated its
inputs without it, so when it ran in the probe plan, it analyzed the reads a
second time, unpruned. `05508_autopr_probe_join_estimate_reuses_index_analysis`
caught this in Fast test on this PR: index analysis rounds and marks checked
differed with and without automatic parallel replicas for six of its queries.

Pass the same option in the walk, so it estimates the same rows as join
optimization does. Locally (debug), 05508 fails 5/5 without this change and
passes 20/20 with it; 04201 passes 20/20.

The preceding merge of master brings in ClickHouse#130516, whose option this needs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 20, 2026
Cherry pick #130516 to 26.10: Estimate the join inputs of the automatic parallel replicas probe from the single-node analysis
robot-clickhouse added a commit that referenced this pull request Nov 20, 2026
…parallel replicas probe from the single-node analysis
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 20, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 20, 2026
Backport #130516 to 26.10: Estimate the join inputs of the automatic parallel replicas probe from the single-node analysis
nickitat added a commit that referenced this pull request Nov 20, 2026
Since #130516 the probe takes its join estimates from the single-node analysis, so the old three-table join already
matches on its first run. Identical subqueries reading the same table cannot be paired with the single-node reads,
so they still have no estimates on a cold run, and the immediate retry is what lets the second run match.

Related: #130565 (review)
Related: #130516

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants