Skip to content

[Performance]: Column statistics never refresh after bulk DML #32202

Closed as duplicate

Description

@Ariznawlll

Environment

  • MatrixOne main@HEAD (c6683bd), 3-CN local cluster
  • Table: stale_t (k INT PRIMARY KEY, g INT NOT NULL, v INT, KEY idx_g(g))

Reproduction

-- 100k rows, then ANALYZE
INSERT INTO stale_t SELECT ... ;  -- 100,000 rows
ANALYZE TABLE stale_t;

EXPLAIN VERBOSE SELECT COUNT(*) FROM stale_t WHERE g = 7;
-- outcnt=1000  (100k rows / 100 groups — precise ✓)

-- now grow the table 10x with identical distribution
INSERT INTO stale_t SELECT ... ;  -- +900,000 rows (same k % 100 pattern)

EXPLAIN VERBOSE SELECT COUNT(*) FROM stale_t WHERE g = 7;
-- TableCnt is refreshed (selectivity recomputed) but outcnt stays pinned:
-- outcnt=1000   ← actual is 10,001 — a 10x underestimate

The estimate does not recover after 75 seconds, hours, or ever — only a manual ANALYZE fixes it.

Root cause (code)

pkg/vm/engine/disttae/txn_table.go:290 (transientTableStats): on every query the engine recomputes the live row count (getLatestPartitionState + partitionRowEstimate) and refreshes only TableCnt and SizeMap. NdvMap, MinValMap, MaxValMap, NullCntMap are copied unchanged from the last ANALYZE snapshot:

func transientTableStats(published *pb.StatsInfo, rows float64) *pb.StatsInfo {
    var stats pb.StatsInfo
    if published != nil {
        stats = *published    // NDV/Min/Max/NullCnt frozen at last ANALYZE
        ...
        stats.TableCnt = rows // only this is refreshed

Consequences:

  1. The visible cost model is internally inconsistent: selectivity shrinks as TableCnt grows while NDV stays stale, so outcnt = TableCnt × selectivity lands on arbitrary values (here 1000).
  2. There is no background re-ANALYZE (no daemon, no staleness detection; StatsInfo has no timestamp field) and PatchStats is only invoked manually from a table function.

Impact

Any INSERT/DELETE-heavy table (orders, logs, events — the tables where stats matter most) silently degrades to stale-NDV estimates between ANALYZEs. Join build/probe sizing, join order, dop and index selection all inherit the error, compounding with the join-NDV issues (#32122 family). Operators get "the plan was good yesterday, slow today" with no tool to notice except manually re-running ANALYZE.

Suggested fix direction

  • Track per-table DML delta since last ANALYZE and trigger async re-ANALYZE past a ratio threshold (like MySQL's innodb_auto_recalc), or
  • At minimum, expose staleness (rows-since-analyze) in mo_table_stats_alpha so the inconsistency is observable.

Activity

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

    @XuPeng-SH
    Contributor

    This issue is being consolidated into #31993, the broader planner-statistics freshness issue. Preserve the distinct stale NDV/min/max/null-count-after-bulk-DML reproduction and ANALYZE control as acceptance coverage in #31993.

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