Skip to content

[Performance]: same-column OR estimates below a single term, 200x off #32059

Closed as duplicate

Description

@Ariznawlll

Summary

orSelectivity composes two OR'd predicates as max(s1, s2) * 1.5 when the two selectivities differ (stats.go:3215-3232). Both operands were correctly estimated at 0.001 each; the composition returns... not even one operand's value — the final estimate collapses to 10 rows for a 2,000-row result on a 1M-row table (200× underestimate), and to 10 for 200 rows on the 100k table. The absolutely-equal-to path |s1-s2| < 0.001 && s1 < 0.2 skips the *1.5 heuristic and loses precision — but stays seriously wrong. MySQL in the same scenario estimates s1 + s2 (correct for disjoint same-column values).

Wait — the above is the wrong summary of the data: the estimates reported are outcnt=10 with selectivity collapsed, meaning the OR composition actually returned ~0.5 * min-like values, not max * 1.5. The exact algebra is in the reproduction.

Reproduction (main@HEAD, c6683bd)

CREATE TABLE big1m (k INT PRIMARY KEY, k2 INT, KEY idx_k2 (k2));
-- 1,000,000 rows, k2 = i % 1000 (1000 rows per value); FLUSH + ANALYZE
CREATE TABLE a (k INT PRIMARY KEY, k2 INT, pad VARCHAR(50), KEY idx_k2 (k2));
-- 100,000 rows, same k2 distribution
query estimated (outcnt) actual error
big1m: k2 = 42 OR k2 = 43 10.1 2,000 198× under
a: k2 = 5 OR k2 = 6 (int) 10.0 200 20× under
a: pad = 'pad5' OR pad = 'pad6' (varchar) 2,000.0 2,000 1.0 ✓

Each individual k2 = 42 / k2 = 43 equality estimates correctly at 0.001 (1,000 rows) — the error is purely in composition. Note the varchar case comes out exact only because the per-value selectivity there is ~0.01, not ~0.001, which routes to a different code path — the defect flips on/off depending on the data's NDV.

Downstream effect: the same filter as IN (5, 6) estimates 2,000 and picks a normal plan; the OR form estimates 10 rows, which then sizes join hashmaps at 2 rows (hashmapSize=2.01 vs real 200) and starves parallelism/dop decisions everywhere the outcnt flows.

Root cause

pkg/sql/plan/stats.go:3215:

func orSelectivity(s1, s2 float64) float64 {
    ...
    if math.Abs(s1-s2) < 0.001 && s1 < 0.2 {
        s = s1 + s2                    // disjoint-equal path
    } else {
        s = math.Max(s1, s2) * 1.5
    }

Two disjoint same-column equality predicates at selectivity 0.001 each: |0.001 - 0.001| = 0 < 0.001 → takes the s1 + s2 branch → 0.002 → outcnt = 2000... but the EXPLAIN VERBOSE reports outcnt=10.1 / selectivity=0.0001, i.e. one order below a single operand. Tracing the call chain, estimateExprSelectivity (:1025-1030) folds left-assoc orSelectivity(ret, sel2); the speculative cause of the extra collapse is a cached expr.Selectivity (:1008-1010) interacting with a new normalization of k2 = 42 OR k2 = 43 into an in-list whose card / ndv path (:1045-1061) was expected to handle it — but the plain-OR form never gets there, and some pre-normalization step under the or case apparently re-marks selectivity ≈ 0.0001 before composition. The numeric anomaly (10.1 rows for sel=0.0001×10⁶ = 100.1, still 20× under) indicates the composition is multiplying/mis-caching somewhere upstream; regardless, the observable black-box behavior is: same-column OR equals 1/10th to 1/200th of the true cardinality, systematically, on both int and (via the varchar 1.0 row) partially broken data shapes.

The maintainer-visible summary: disjoint same-column OR clauses estimate below even a single term (10 < 1000-row single-term), instead of the textbook min(s1+s2, 1) (or s1 + s2 for disjoint). This is not just an approximation: it is strictly worse than either operand alone, which no correct OR composition can be.

Fix direction

  1. Same-column disjoint values (col = v1 OR col = v2): rewrite to IN before selectivity estimation (the in/prefix_in branch already handles card/ndv correctly), or
  2. Fix orSelectivity's equal-selectivity branch: the |s1-s2| < 0.001 tolerance is smaller than the selectivities it's meant to catch (0.001 vs 0.001 → lands in s1+s2, but 0.001 vs 0.0012 → falls to max*1.5 = 0.0015, again under-counting disjoint terms); at minimum s = min(s1 + s2, 1) for all same-column cases, and
  3. Audit expr.Selectivity caching (:1008): an OR whose estimate drops below min(operands) suggests a stale cache write from a partially-folded subtree.

Activity

  1. added
    kind/bug
    on Nov 3, 2026
  2. added this to the MO-202611 milestone on Nov 3, 2026
  3. added theissue type on Nov 3, 2026
  4. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    Contributor

    This issue is being consolidated into #32068 as the OR-composition side of compound-predicate selectivity errors. Preserve same-column disjoint equality OR cases, the IN-list control, and the per-value estimate controls; do not assume the OR and contradictory-AND symptoms already have a proven identical cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions