Skip to content

Plan a distributed query once for all remote shards - #130655

Merged
KochetovNicolai merged 2 commits into
ClickHouse:masterfrom
eoger-ant:perf/plan-distributed-query-once
Nov 18, 2026
Merged

KochetovNicolai merged 2 commits into
ClickHouse:masterfrom
eoger-ant:perf/plan-distributed-query-once

Conversation

@eoger-ant

@eoger-ant eoger-ant commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Related: #77144
Related: #60497
Related: #85718

Changelog category (leave one):

  • Performance Improvement

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

Speed up planning of distributed queries with many shards: the initiator plans the query once for all remote shards, not once for each of them (unless serialize_query_plan is enabled).

Description

For each remote shard, the initiator plans the query again to get the header of the shard's result, and the planner context for filters that are pushed down. With a complex query and many shards, this can take seconds (#77144).

It has been like that since #60497 (24.3): Object columns could have different types on the shards, so the header could differ. The type was removed in #85718.

Now shards with the same query tree share its text, header and planner context. Shards have their own trees, and are planned separately as before, if IN may be rewritten for each of them (optimize_skip_unused_shards_rewrite_in). So are they with serialize_query_plan, where each has its own plan to send.

The overload of createForShard that takes an AST had no callers, so it is removed.

Time of EXPLAIN for the query of the added performance test, in milliseconds:

1 shard 8 shards 32 shards
24.2.1, allow_experimental_analyzer = 1 13 15 20
24.3.1 16 31 83
26.10.2 19 40 109
master, local build 28 58 158
this PR, local build 28 29 32

The second part of #77144, addFilters, is not changed.

Tests: 05534_distributed_query_planned_once_for_all_shards, tests/performance/distributed_planning.xml.


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

Version info

  • Merged into: 26.11.1.2732-master (included in 26.11 and later)

The header that a remote shard returns, and the planner context for filters that
are pushed down, were calculated by planning the query again for each shard.
Shards could differ in the types of `Object` columns, which do not exist any more.
@clickhouse-gh clickhouse-gh Bot added comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). external Author is not a member of the ClickHouse organization labels Nov 11, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [07be589]

Summary: ✅


AI Review

Summary

Reviewed PR #130655 at 07be589d9f3b433c1699875314e9868825ac7718. The change shares query trees, result headers, and planner contexts across equivalent remote shards while preserving separate planning for shard-specific IN rewrites and serialized plans. No actionable findings. The description includes before/after measurements, and the added functional test passed in Fast test CI. Local tests were not run.

Final Verdict

✅ 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 07be589d9 with master abc7ede4c (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

27 translation units recompiled, 355 s compile time in total, 27 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 -22.5 s (-6%) in total before that adjustment.

Job report

@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Nov 18, 2026
Merged via the queue into ClickHouse:master with commit f63ca4f Nov 18, 2026
192 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
alexey-milovidov added a commit to groeneai/ClickHouse that referenced this pull request Nov 18, 2026
ClickHouse#130655 and `fa76a947810d` ("Do not use inactive
replicas as `make_distributed_plan` workers") on `master` renamed the static
`getActiveReplicasForParallelReplicas(context, cluster)` to the exported
`getReplicasLiveness(cluster_name, cluster)`. The merge kept the new name, but the call in
`getActiveReplicasCountForParallelReplicas` added by this PR still used the old one. Pass the
`cluster_for_parallel_replicas` setting as the cluster name, which is what the old function read.

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

can be tested Allows running workflows for external contributors comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). external Author is not a member of the ClickHouse organization 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.

4 participants