Repository navigation
Conversation
… class Without NDV statistics, `estimateJoinedRows` assumes FK->PK for every equi join pair separately and keeps `max(lhs, rhs)`. In TPC-H Q5, `c_nationkey = s_nationkey AND s_nationkey = n_nationkey` puts `customer` (1.5M rows) and `supplier` (100K rows) in one key class with the 25-row `nation`, so joining `customer` with `supplier` on that key is costed as 1.5M rows while it produces ~6e9. Once the hash table statistics of a first run give every input a size, the optimizer picks that join and every repeat run of Q5 times out. Apply the FK->PK assumption consistently within the class: its smallest relation (over all members, also those on neither side) holds the unique key, so the class has at most that many distinct values. When the two sides are connected by exactly one join key that is such a class, the estimate is `max(lhs, rhs, lhs * rhs / class_min)`. Composite keys, a side without a row estimate and reliable NDV keep today's rules. An equi condition counts as another join key only when it connects the two sides and its operands are not in one class. DPsub keeps its per-candidate work on native masks: the smallest class member is precomputed once per solve and the side test of the extra-key count runs on `toMask` of the edge sources. Gated by the new setting `query_plan_join_order_equivalence_class_ndv_bound` (default true, `compatibility <= 26.10` restores the old estimate). Closes ClickHouse#128094 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…timate `ON ... AND c_nationkey = s_nationkey AND (c_custkey <=> s_suppkey)` connects `customer` and `supplier` by two keys, one of which (`NullSafeEquals`) is in no key class, so the equivalence-class NDV bound must not apply and the join keeps the `max(lhs, rhs)` estimate. Arms under greedy and dpsub. With the guard in `countUnclassifiedEquiKeys` disabled both arms switch to the all-hints plan (`customer` joined with `orders` first), so they exercise the guard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the setting-pointer and loop-scope comments, shorten the applyEquivalenceClassBound and countUnclassifiedEquiKeys contracts to one line each. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 runs, 2 findings, the re-review of the final head found none) plus my own review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261029-021700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261029-020900 |
|
cc @davenger @vdimir, could you review this and add the |
|
Workflow [PR], commit [49a8c73] Summary: ❌
AI ReviewSummaryThe PR improves join ordering without NDV statistics by estimating single-key joins against the smallest member of the key's equivalence class. Review of head Missing context / blind spots
Final VerdictStatus: ✅ Approve. No required code changes identified. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes55 object files changed (+48.55 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units162 translation units recompiled, 1470 s compile time in total, 162 of them have a recent master baseline. |
`tests/performance/one_thousand_joins.xml` got 55-62% slower with the class bound on (CI, amd and arm). Every pair the join order search evaluated rescanned its applicable edges with `asBinaryPredicate` (a function name copy plus operator name comparisons), `resolveAliases` and two class lookups to count the keys outside a class, and the greedy/DPsize/DPhyp path looked up the row count of every class member, including members on neither side. Both depend only on the graph, so `QueryGraph::buildEquivalenceClassBounds` now computes the smallest size of each class and the set of equi edges outside a class once, before the search. A pair evaluation does one hash lookup per class and, only when such an edge exists, one per applicable edge. Plans are unchanged. Debug build, the perf test query in `clickhouse local`, setting 0 -> 1: before 2.9 -> 8.0 s (dpsub), 10.3 -> 16.0 s (dphyp), 9.0 -> 14.9 s (dpsize); after 2.8 -> 2.8, 8.6 -> 8.5, 9.2 -> 9.4 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 49a8c73Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The Session id: cron:our-pr-ci-monitor:20261029-203004 |
Base-table NDV can exceed the row count of a multi-relation join input, so `1 / max(NDV)` underestimates the output of the join above it and the optimizer picks a worse order. Cap each join key NDV and NDV upper bound by the estimated rows of its input subtree when that side contains more than one relation. `SelectivityCache` stores context-independent `JoinKeyStats` instead of a finished `SelectivityEstimate`: the same edge can join different subtrees in another candidate plan, so the row cap is applied after the cache lookup. Both direct predicates and transitive equivalence classes are covered in `dpsub`, `dphyp`, `dpsize`, and `greedy`. Tests: `05493_join_order_subtree_ndv` checks corrected estimates, cache contexts, and bushy plans. `05494_join_order_subtree_ndv_stable` uses independent tables per algorithm to verify cold and warm `EXPLAIN` plans are identical with hash table statistics enabled. Historical benchmark (7 runs, discard the first, median of the remaining 6; result hashes identical): TPC-DS Q72 0.1725s -> 0.0835s. TPC-H Q02 unchanged at 0.034s. These figures come from the original benchmark logs and were not rerun for this commit. Related: ClickHouse#128102 Related: ClickHouse#114614 Related: ClickHouse#101398
|
Without statistics, all bets are off ("garbage in - garbage out"). In Berlin, the Join team agreed to ignore this case. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
When join keys have no NDV statistics, the join order optimizer assumes that the smallest of all tables joined on one key holds the unique key, so a join of two larger tables on that key is estimated as many-to-many instead of as a key lookup. Repeat runs of TPC-H Q5 on tables without column statistics no longer time out. Controlled by the new setting
query_plan_join_order_equivalence_class_ndv_bound(enabled by default).Description
Closes: #128094
Without NDV statistics,
estimateJoinedRowscosts every equi join with both sizes known as FK->PK,max(lhs, rhs), one pair at a time (#120176). In TPC-H Q5,c_nationkey = s_nationkey AND s_nationkey = n_nationkeyputscustomerandsupplierin one key class with the 25-rownation, socustomer x supplieron that key is costed 1.5M rows instead of ~6e9. The first run plans well because the filtered inputs have no size; once the hash table statistics of that run give every input a size, the optimizer picks this join and every later run times out.This PR applies the assumption to the whole key class: its smallest relation (also on neither side) bounds its distinct values, so for exactly one connecting key the estimate is
max(lhs, rhs, lhs * rhs / class_min). Composite keys, a side without a size and reliable NDV keep today's rules. Both selectivity paths (greedy/DPsize/DPhyp and DPsub) are changed.compatibility <= '26.10'or the setting= 0restores the old plans.Validation:
05331_join_order_one_side_unknown_rowsgets greedy and dpsub arms with all sizes known (old plan on master with all four algorithms, new with the patch), the same arms with an extra<=>key (a composite key, old plan kept) and a setting-off control. Synthetic TPC-H SF10 without statistics (debug build, 4 threads): Q5 repeat runs 9/9 overmax_execution_time = 60-> about 1 s, Q5 without filters 3/3 over the limit -> about 3 s, first runs unchanged (same EXPLAIN), same results. The issue author measured TPC-DS without NDV: 151.0 -> 150.6 s total, q65 +3.6% (its GROUP BY key is not known to be unique). This PR's Performance Comparison will not show it:tpch.xmlmaterializes column statistics, and the Iceberg suites run withoutuse_iceberg_manifest_statistics(Q5 about 0.8 s on master).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128102]