Repository navigation
Conversation
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
|
@cv4g PTAL,thanks. |
|
Workflow [PR], commit [bdb063c] Summary: ❌
AI ReviewSummaryReviewed #129663 at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No outstanding review findings; normal CI checks still need to complete. |
…o cardinality-squashed # Conflicts: # src/Processors/QueryPlan/Optimizations/joinOrderCommon.h # src/Processors/QueryPlan/Optimizations/joinOrderDPSub.cpp
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1413 translation units recompiled, 54 s compile time in total, 1413 of them have a recent master baseline. |
`1 / max(NDV)` assumes that the keys of the side with fewer distinct values are contained in the other side. Capping a multi-relation input below the other side's NDV reverses that assumption: a filtered sample of a large key domain, such as `customer` joined with a filtered `nation`, was assumed to match `orders.o_custkey` completely. In TPC-H Q7 this raised the estimate of `orders ⋈ (customer ⋈ n2)` from 1.2 million (actual 1.2 million) to 1.8 million, and the optimizer picked `orders ⋈ (lineitem ⋈ (supplier ⋈ n1))` first. That is the `tpch-join_algorithm-partial_merge` ClickHouse#5 regression. Cap a side only while its input still has at least as many rows as the other side has distinct keys. Plans of all TPC-H queries at SF10 are now the same as on `master`, so the `03836_tpch_join_order_plans` reference returns to the `master` version, and the correlated cases in `05493_join_order_subtree_ndv` keep their capped estimates. Add a case where the subtree is a sample of the key domain. Also use `UnorderedMapWithMemoryTracking` for `SelectivityCache` again, as `master` requires for `src/Processors/QueryPlan`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=129663&sha=ac91fe8f1912ba540f0cec2155ad89f0c7411cc2&name_0=PR PR: ClickHouse#129663
AI review of a later commit does not approve the PR, see the AI Review comment.
When the opposite key lacks NDV statistics but has a known row-count upper bound, apply the same containment guard to both the NDV used for costing and the reported upper bound. Add direct and transitive partial-statistics regressions. ClickHouse#129663
…o cardinality-squashed Keep per-candidate selectivity in `EnumeratorCheckerWithCosts::acceptMergeJoins` and pass `key_classes` through to `mergeJoinCost`. Adapt the enumerator checker test to that signature.
Description
Join reordering estimates an equi join using
1 / max(NDV). When a join key originates from a base table inside an already joined subtree, its base-table NDV can exceed the estimated row count of that subtree. This underestimates the output cardinality of the join above it and can lead to a worse join order.Cap both the NDV and its upper bound by the estimated row count of the input subtree when that side contains more than one relation. Single-relation inputs retain their existing estimates.
Example
Before this change is:
After this change:
Previously, the outer join used selectivity
1 / max(100, 1000) = 0.001, underestimating its output as 1,000 rows.Capping
d2.kby the 100-row input subtree changes the selectivity to1 / max(100, 100) = 0.01and the estimated output to 10,000 rows, matching the actual cardinality.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improve cardinality estimation for joins above multi-relation subtrees by capping join-key NDV by the estimated input row count.
Workflow [PR]
Sync PR [sync-upstream/pr/129663]