Skip to content

perf: TPC-H Q18 单机退化 14x — PR #25822 (hash MARK join) 编译路径使用 loop join 而非 hash join #26132

Closed

Description

@Ariznawlll

问题描述

PR #25822 (fix(sql): reimplement hash MARK join for IN subqueries) 导致 TPC-H 单机性能严重退化。

退化幅度

Scale 退化前 (07-17) 退化后 (07-18) 最新 (07-24) 主要退化 Query
1T ~1,009s ~2,189s 2,693s Q18: 85s → 1,240s (14x)
100G ~84.5s ~104.5s 134.6s Q16: 0.6s→3.7s (6x), Q14: 0.7s→3.7s (5x)
10G ~8.9s ~14.5s 17.0s Q18: 0.23s→1.4s (6x), Q16: 0.26s→0.9s (3.4x)

TKE 集群 (3CN) 退化幅度明显更小:1T 仅 ~10% (Q18 1.6x),10G/100G 无退化。

根因分析

执行计划变化

PR #25822 将 IN 子查询(如 Q18 的 o_orderkey IN (SELECT l_orderkey FROM lineitem GROUP BY l_orderkey HAVING sum(l_quantity) > 314))从 SEMI join 改为 MARK join。

编译路径差异 (pkg/sql/compile/compile.go)

SEMI join → Hash Join(O(P+B)):

// compileShuffleJoinV2 line 3309
case plan.Node_INNER, plan.Node_LEFT, plan.Node_RIGHT, plan.Node_SEMI, plan.Node_ANTI, plan.Node_OUTER:
    op := constructHashJoin(...)  // ← 高效 hash join

MARK join → Loop Join(O(P×B)):

// compileShuffleJoin line 3505
case plan.Node_MARK:
    rs = c.newProbeScopeListForBroadcastJoin(...)   // ← broadcast 而非 shuffle
    op := constructLoopJoin(...)                     // ← 嵌套循环 join

compileShuffleJoinV2 的 switch case 中 没有 plan.Node_MARK,导致 MARK join 无法走 shuffle hash join 路径,退化为 broadcast loop join。

Loop Join 的 O(P×B) 嵌套循环

// pkg/sql/colexec/loopjoin/join.go line 199-224
for i := ctr.probeIdx; i < count; i++ {          // 每个 probe 行
    for idx := 0; idx < len(mpbat); idx++ {       // 遍历所有 build batch
        for j := uint64(0); j < vec.Length(); j++ { // 每行做条件求值
            // O(P × B) 总操作数
        }
    }
}

TPC-H Q18 数据量 (SF=1000)

  • Build 侧(子查询结果):~50K 行
  • Probe 侧(orders 表):1.5 亿行
  • Hash Join: O(P+B) ≈ 1.5 亿次操作
  • Loop Join: O(P×B) ≈ 7.5 万亿次操作

为什么单机退化远大于分布式

              SEMI (旧)                    MARK (新)
单机 1CN:  broadcast + HASH JOIN         broadcast + LOOP JOIN
           O(P + B) = 85s               O(P × B) = 1,240s  → 14x 🔴

TKE 3CN:   shuffle + HASH JOIN          broadcast + LOOP JOIN
           O(P/3 + B/3) = 165s          O(P/3 × B) = 268s  → 1.6x ⚠️
  • 单机:只有 1 个 CN,O(P×B) 全部由一个节点承担,无法用并行缓解
  • TKE:3 个 CN 并行处理,每个只承担 O(P/3 × B),部分弥补了算法劣势

复现

128 核单机 Binary Search 测试(commit 51098d6)也能复现:

  • Q18: 230ms → 702ms (3.1x) — 在更强硬件上仍然退化,证明是算法问题
  • Q16: 260ms → 406ms (1.6x)

建议修复

方案 1(推荐):在编译路径为 MARK join 启用 hash join

在 compileShuffleJoinV2 / compileShuffleJoin / compileProbeSideForBroadcastJoin 中为 plan.Node_MARK 使用 constructHashJoin 而非 constructLoopJoin。

Hash join operator 已通过 PR #25822 添加了 MARK join 支持(TrackNullKeys、IsMark()、validateMarkJoin()),但编译路径没有路由到它。

方案 2:规划阶段回退

当检测到 shuffle 场景时,将 MARK join 降级回 SEMI join + null key tracking 的方案。

验证

修复后应验证:

  • TPC-H Q18 在 1T scale 回到 ~85s(单机)
  • TPC-H Q16 在 100G scale 回到 ~0.6s
  • 所有 IN/NOT IN 子查询的正确性测试通过

相关

Activity

  1. self-assigned this
    on Jul 24, 2026
  2. added
    kind/bug
    on Jul 24, 2026
  3. added theissue type on Jul 24, 2026
  4. XuPeng-SH commented on Jul 24, 2026

    @XuPeng-SH
    Contributor

    Controlled same-data A/B does not reproduce the claimed SF10 Q18 regression, and the MARK-join root-cause attribution is incorrect.

    Controlled setup

    • Same standalone machine, GOMAXPROCS=10
    • Same etc/launch/launch.toml (identical SHA-256)
    • Same existing mo-data / tpch_10g tables and statistics
    • Same canonical Q18 SQL (HAVING sum(l_quantity) > 314)
    • Exact 0d8111aef18ed25... rebuilt from source in a detached worktree
    • Current clean main binary: d8a464eb65
    • Because current catalog metadata is 4.0.5 while 0d8111ae declares 4.0.1, the old benchmark build only skips the startup downgrade guard. No planner, compiler, shuffle, group, join, scan, or runtime-filter code was changed.

    Each binary received 3 warm-up executions followed by 15 timed executions.

    binary median mean min-max
    0d8111ae 0.40 s 0.409 s 0.38-0.48 s
    clean main d8a464eb65 0.40 s 0.405 s 0.39-0.47 s

    Both plans are the same relevant shape:

    Join Type: SEMI
    Join Cond: orders.o_orderkey = lineitem.l_orderkey
      -> Aggregate
           Group Key: lineitem.l_orderkey shuffle: range(lineitem.l_orderkey)
           inputRows=15000000 outputRows=84
           MemorySize=1.09 GiB
           -> lineitem scan inputRows=59986052
    

    Q18 is not planned as MARK on either commit. PR #25822 also explicitly states: “ordinary filter-only IN already uses SEMI join planning.” Therefore the proposed chain Q18 IN -> MARK -> broadcast LoopJoin does not occur for this query, and adding MARK to the shuffle compiler cannot explain Q18. Q16 is likewise ANTI under the standard NOT NULL TPCH schema, not MARK.

    Do not compare cross-version EXPLAIN ANALYZE timeConsumed directly here: #25843 changed SQL resource-accounting/analyzer semantics between these commits. Wall-clock under controlled A/B is the reliable comparison.

    The reported historical SF1000 slowdown may still be a real scale-dependent spill/shuffle regression, but it needs a real before/after Q18 physical plan, spill counters, CN memory, and exact runner/config evidence. It should be investigated under the existing Q18 aggregation/shuffle work (#20560; historical spill failure #24836), not attributed to #25822 without a plan showing MARK. The SF10 claim (0.23 s -> 1.4 s) is not reproducible under controlled same-data testing.

  5. XuPeng-SH commented on Jul 24, 2026

    @XuPeng-SH
    Contributor

    Follow-up with the two requested 128-runner workflow runs confirms a real Linux/amd64 performance regression, while still refuting the MARK-join attribution.

    Compared runs:

    • current 9c2e84ad5c: run 30065338834, TPCH job 89395726892
    • baseline 0d8111aef1: run 30067127559, TPCH job 89400835066

    The runs are well controlled:

    • same workflow (Binary Search On 128 New)
    • same 128-runner label
    • same build flags: GOAMD64=v3 GOEXPERIMENT=simd
    • executed only ~41 minutes apart
    • fresh load from the same TPCH data paths
    • load stage was nearly identical: ~516 s for current vs ~513 s for baseline
    • no OOM, spill error, timeout, panic, or query-result failure in either run

    Using turns 2-5 (excluding the first cold turn):

    Scale/query current 9c2e84 baseline 0d8111 regression
    SF10 Q18 683.8 ms 187.3 ms 3.65x
    SF100 Q18 6701.8 ms 3245.5 ms 2.06x
    SF10 sum of Q1-Q22 8117.8 ms 5115.0 ms 1.59x
    SF100 sum of Q1-Q22 107619 ms 81580 ms 1.32x

    This is broad rather than Q18-only. Other large warm regressions include:

    • SF10: Q10 2.32x, Q16 2.22x, Q14 1.89x, Q20 1.67x, Q17 1.63x
    • SF100: Q16 2.78x, Q20 1.66x, Q10 1.55x, Q22 1.54x, Q14 1.48x, Q15 1.46x, Q9 1.40x

    Counterexamples are important: SF100 Q1/Q6/Q19 are unchanged or slightly faster on current. So this is not a uniform runner-frequency or storage slowdown.

    Conclusion:

    1. The 128-core Linux regression between 0d8111ae and 9c2e84ad is confirmed.
    2. It is not caused by Q18 becoming MARK: Q18 remains SEMI, and PR fix(sql): reimplement hash MARK join for IN subqueries #25822 explicitly preserves SEMI for filter-only IN.
    3. The affected set points toward a shared high-parallelism shuffle/group/join execution change. The commit interval includes shuffle unification (refactor: unify shuffle implementations #25814), scheduler worker allocation (scheduler: add pool-aware deterministic worker subsets #25990), fixed-bucket/local-shuffle backpressure and lifecycle changes (fix: isolate fixed-bucket shuffle backpressure #26017/fix: prevent nested local shuffle deadlocks #26055/fix: decouple local shuffle producers from consumers #26097), and resource-accounting changes (refactor: unify SQL resource accounting semantics #25843). This is a candidate list, not attribution; binary search plus physical-plan/operator profiling is required.
    4. The earlier local arm64 SF10 A/B did not reproduce because this appears architecture/high-DOP sensitive; it must be diagnosed on the 128-core Linux runner.

    Please retitle/reframe this issue as a broad TPCH high-DOP regression and remove the unsupported MARK/LoopJoin causal claim. Keep Q18 as the strongest symptom, but do not implement a MARK-specific Q18 fix.

  6. XuPeng-SH commented on Jul 24, 2026

    @XuPeng-SH
    Contributor

    Third 128-runner comparison: run 30068844288 built MatrixOne 7d4a3e84e5 (the #25822 commit). Same methodology: turns 2-5 only.

    Scale/query baseline 0d8111ae 7d4a3e84 current 9c2e84ad 7d/base current/7d
    SF10 Q18 187.3 ms 679.3 ms 683.8 ms 3.63x 1.01x
    SF100 Q18 3245.5 ms 7184.5 ms 6701.8 ms 2.21x 0.93x
    SF10 Q1-Q22 sum 5115 ms 7128 ms 8118 ms 1.39x 1.14x
    SF100 Q1-Q22 sum 81580 ms 96815 ms 107619 ms 1.19x 1.11x

    This proves that the Q18 regression is already fully present at 7d4a3e84; later changes through 9c2e84ad did not worsen Q18 (SF100 is actually ~7% faster). Q16 is also mostly regressed by this point: SF10 2.12x and SF100 2.57x versus baseline.

    However, this still does not isolate #25822. 0d8111ae..7d4a3e84 contains 32 commits. 7d4a3e84 is merely the endpoint of that interval. Its direct parent is:

    7ffd26c7e167ff4b5fc3ce01313c49101116ea9b
    

    The decisive experiment is 7ffd26c7e1 versus 7d4a3e84e5 on the same 128-runner. Only that adjacent-commit A/B can attribute the regression to #25822.

    The three runs also show two phases:

    1. By 7d4a3e84, SF10 is already 1.39x and SF100 1.19x slower overall; Q18/Q16 carry their full regression.
    2. From 7d4a3e84 to 9c2e84ad, another ~11-14% suite regression appears, concentrated differently (for example SF100 Q9/Q14/Q15/Q20/Q22), while Q18 is stable or better.

    So continue the binary search, but use the direct parent before changing the issue root cause or implementing a MARK-specific fix.

  7. XuPeng-SH commented on Jul 24, 2026

    @XuPeng-SH
    Contributor

    Decisive adjacent-commit A/B is now available for SF10:

    • parent: 7ffd26c7e167ff4b5fc3ce01313c49101116ea9b, run 30069979831 / job 89409387514
    • PR commit: 7d4a3e84e552f5295ed5ca8f92a34f9c510d723f, run 30068844288 / job 89405897951

    Same 128-runner workflow; turns 2-5 warm average:

    Metric parent 7ffd26c7 PR 7d4a3e84 PR/parent
    SF10 Q1-Q22 sum 7153.3 ms 7128.0 ms 0.996x
    SF10 Q18 717.0 ms 679.3 ms 0.947x
    SF10 Q16 376.5 ms 390.3 ms 1.037x
    SF10 Q10 571.5 ms 566.0 ms 0.990x

    No query regressed by more than ~6%; changes are normal run variance. Q18 is ~5.3% faster after #25822, not slower. The whole suite is effectively unchanged/slightly faster.

    Therefore #25822 is definitively not the cause of the observed SF10 regression. The regression already exists in its direct parent. Compared with the good 0d8111ae baseline:

    • parent suite: 7153.3 vs 5115.0 ms = 1.40x slower
    • parent Q18: 717.0 vs 187.3 ms = 3.83x slower
    • parent Q16: 376.5 vs 184.5 ms = 2.04x slower
    • parent Q10: 571.5 vs 312.5 ms = 1.83x slower

    The correct regression range is now 0d8111ae..7ffd26c7 (31 commits), excluding #25822. Continue binary search inside that range. A midpoint around d8ca64575d would split it roughly in half.

    Important limitation: run 30069979831 intentionally ran SF10 only; SF100/1T were skipped. This adjacent A/B settles the claimed SF10 regression, but a direct-parent SF100 run is still needed before making an empirical high-scale claim. The plan-semantics evidence remains unchanged: canonical Q18 is SEMI, not MARK.

    The issue title/root-cause section should now be corrected; keeping #25822 as the blamed change contradicts direct adjacent-commit evidence.

  8. added 2 commits that reference this issue on Jul 24, 2026
    2c29244
    36f8b76
  9. XuPeng-SH commented on Jul 28, 2026

    @XuPeng-SH
    Contributor

    fixed

  10. Ariznawlll commented on Jul 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新单机main commit:3c877f635be75b918f4e01e1a8abf01c77dfdd19 tpch性能已恢复

    job:https://github.com/matrixorigin/mo-auto-test/actions/runs/30383489176

  11. added 6 commits that reference this issue on Sep 17, 2026
    9c806d8
    6c481cb
    cb9525a
    27aa641
    555b2c4
    22ea5a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugperformanceseverity/s-1

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions