Skip to content

[Performance]: IN-subquery semijoin is 27x slower than equivalent JOIN #32013

Closed

Description

@Ariznawlll

Summary

WHERE col IN (SELECT ...) plans a SEMI join with a runtime-filter build→probe dependency whose synchronization costs dominate the query: at 1M outer rows it is 26.6× slower than the equivalent explicit JOIN, with identical results. The gap is super-linear (4.5× at 100k → 26.6× at 1M).

Environment: main@HEAD (c6683bd), etc/launch-multi-cn, tables ANALYZEd.

Reproduction

-- big1m: 1,000,000 rows, k2 = i % 1000, KEY idx_k2(k2)
-- b:       100,000 rows, grp = i % 50000, KEY idx_grp(grp); predicate grp < 100 hits 200 rows

SELECT COUNT(*) FROM big1m WHERE k2 IN (SELECT grp FROM b WHERE grp < 100);
-- 373.2 ms (median of 5)

SELECT COUNT(*) FROM big1m JOIN (SELECT DISTINCT grp FROM b WHERE grp < 100) t ON big1m.k2 = t.grp;
-- 14.1 ms (median of 5)   — same result: 100000

EXPLAIN ANALYZE shows the IN form running Join Type: SEMI with Runtime Filter Build: serial_full, and the outer scan waiting on the filter (waitTime=24ms at 100k scale) before probing — the serialization stalls the pipeline while the explicit-JOIN form hash-builds 200 rows and streams the outer table. NOT IN (MARK join) shows the same pattern with a smaller penalty (23.8 ms vs 14.0 ms for NOT EXISTS at 1M).

Expected

The semijoin should prefer a hash semi-join over runtime-filter probing when the build side is tiny (200 rows vs 1M), or at minimum the runtime-filter wait should never exceed the cost of just scanning and hashing. A 26× regression against the hand-written equivalent means every ORM/framework emitting IN (SELECT ...) (the dominant idiom) hits this path.

Activity

  1. added
    kind/bug
    on Nov 2, 2026
  2. added this to the MO-202611 milestone on Nov 2, 2026
  3. added theissue type on Nov 2, 2026
  4. added
    severity/s0
    and removed on Nov 5, 2026
  5. added a commit that references this issue on Nov 5, 2026
    d37c4ba
  6. Ariznawlll commented on Nov 6, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    MatrixOne 官方 main 65b721e1b93c6b9e05db2063b220e0c35916b335,本地隔离部署:2 CN、1 TN、LogService 和 Proxy;CN 及 Proxy 三个入口分别测试。修复 PR #32231 的提交 d37c4ba5e4 已包含在该版本。

    原问题验证

    构造并持久化 100 万行低基数探测表、10 万行子查询表,收集 FULLSCAN 统计信息;分别测原 IN 子查询和语义等价的 DISTINCT JOIN,每组预热 3 次、交替测量 10 次。三个入口的结果均为 100,000 行,IN/JOIN 中位耗时比分别为 1.15、1.07、0.97,本地未复现报告中的约 26.6 倍差距。

    Happy Path 覆盖

    • 10 万行与 100 万行两种规模均验证结果;低基数探测列避免回表索引扫描,高基数对照仍使用索引并返回预期 99 行。
    • 在小表显式 IGNORE INDEX (idx_grp)、设置 AP 执行的控制场景下,物理计划确认为 multicn 并出现跨 CN 接收;百万行三入口 IN/JOIN 中位数比分别为 1.02、1.03、1.04。该控制查询与原查询不同,不能将它当作原查询天然跨 CN 的证据。

    Unhappy Path 覆盖

    空子查询返回 0;NOT IN 返回 900,000。三入口的两种规模和两种执行模式均核对精确计数。上述为边界结果验证,不代表资源异常或所有数据分布均已覆盖。

    白盒验证

    核对 #32231 修改了 join 索引代价中 probe fanout 的估算,并新增 planner 回归及 optimizer BVT;本地计划显示低基数案例不走问题回表路径,高基数对照保留索引路径。

    回归测试

    主库现有 nopk_index_mem_runtime_filter 标准 MOTR:55/55,两个 CN 和 Proxy 各重复 3 次。百万行专项脚本在隔离 nightly 工作区准备中,三个本地入口均执行通过;专项已提交至独立 nightly PR #1910,PR 必需 CI 通过;未向 MatrixOne 主库提交测试 PR。

    测试证据

    固定版本、样本数据、完整计划及每次计时保存在本地专项 JSON 报告。原始原查询为单 CN;跨 CN 结果来自上述显式调整后的小表查询。局部性能比仅反映此机器与该工作负载,不是产品 SLA。

    TKE 专项 run 42162672347 已完成且清理作业成功,工作流解析固定产品 SHA 65b721e1b93c6b9e05db2063b220e0c35916b335,产品 git_version() 返回短 SHA 65b721e。100 万行原查询 IN/JOIN 中位数为 4.879 / 4.502 ms,比例 1.084;强制跨 CN 控制为 7.372 / 7.478 ms,比例 0.986,物理计划显示 multicn 及跨 CN receiver。各场景 3 次预热、交替 10 次测量;高基数索引、空集和 NOT IN 控制通过,完整样本与计划见运行产物 issue-32013-semijoin-42162672347-1。控制查询通过对小表使用 IGNORE INDEX 才触发跨 CN,不能当作原查询天然跨 CN。

    测试结论

    本地及隔离 TKE 的百万行正确性与性能专项通过,未复现原报告的约 26.6 倍退化;专项回归 PR 已提交且 CI 通过,按约定关闭。相对阈值 10 倍是针对原问题设置的保守回归保护,并非产品 SLA;本次不代表所有数据分布或更大规模均已验收。

  7. Ariznawlll commented on Nov 6, 2026

    @Ariznawlll
    CollaboratorAuthor

    专项回归已提交到独立 nightly 仓库 mo-nightly-regression #1910,不涉及 MatrixOne 主库 PR。固定本次 MO SHA 的隔离 TKE 专项运行 42162672347 和清理均已成功;PR 必需 CI 通过。完整数值及测试范围见上方更新后的验收评论。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions