Skip to content

[Performance]: NOT EXISTS anti-join falls back to nested-loop execution #26797

Closed

Description

@XuPeng-SH

Is there an existing issue for performance?

  • I have checked the existing issues.

Environment

  • Version or commit-id: 08770e1beeb23239c91165492f2fe85243a3c66a (main)
  • Hardware parameters: 16 vCPU, 31 GiB RAM
  • OS type: Debian Linux, x86_64
  • Service: local single-node CN/TN on 127.0.0.1:6001
  • Fileservice memory cache: 6GB in both CN and TN

Details of Performance

A correlated NOT EXISTS anti-join is compiled into a LoopJoin when the equality predicate is represented as ((o.campaign_id = c.customer_id) IS TRUE). On a non-partitioned workload this produces nested-loop behavior and scales approximately with outer rows × build rows.

Workload:

  • dim_customer: 200,000 rows, primary key customer_id
  • fact_orders: 750,000 rows
  • fact_orders.campaign_id: 681,819 non-NULL values, 500 distinct values, plus NULLs
  • fact_orders has secondary indexes including (campaign_id, order_status)

Query:

SELECT COUNT(*)
FROM dim_customer c
WHERE NOT EXISTS (
    SELECT 1
    FROM fact_orders o
    WHERE o.campaign_id = c.customer_id
);

Observed behavior:

  • Full query did not return within 180 seconds.
  • EXPLAIN shows an ANTI join with table scans on both sides and condition ((o.campaign_id = c.customer_id) IS TRUE).
  • With an outer predicate selecting only absent customer IDs:
    • 10 outer rows: about 112 ms
    • 1,000 outer rows: about 6.4 s
    • 10,000 outer rows: exceeded 60 s
  • A semantics-preserving control using a pre-deduplicated inner relation completed immediately and returned the expected 199,500 rows:
SELECT COUNT(*)
FROM dim_customer c
LEFT JOIN (
    SELECT DISTINCT campaign_id
    FROM fact_orders
    WHERE campaign_id IS NOT NULL
) x ON x.campaign_id = c.customer_id
WHERE x.campaign_id IS NULL;

The NOT IN form was used only as a NULL-semantics control; because the inner relation contains NULL, its result is correctly 0 and it is not a performance-equivalent query.

A 30-second CPU profile captured while the full query was running spent the dominant join time in pkg/sql/colexec/loopjoin.(*container).probe, with repeated boolean equality evaluation and bitmap checks.

Likely white-box cause: the physical compiler's equi-join detection and hash-condition extraction recognize only a top-level equality, while the flattened anti-subquery condition is wrapped by IS TRUE; this routes an equality anti-join to LoopJoin instead of HashJoin.

Expected behavior is a hash anti-join or another plan with bounded join complexity, while preserving NOT EXISTS NULL semantics. This should be fixed generically, without SQL-text or dataset-specific rules.

Additional information

The issue was found by a BVT-style, non-partition workload with independent count/conservation/metamorphic oracles. Evidence artifacts include:

  • EXPLAIN: anti join + full scans
  • CPU pprof: loopjoin.probe
  • correctness check: 25 checks passed; the anti-join check failed by timeout at 180 s
  • no service errors or correctness mismatches in the surrounding workload

Activity

  1. self-assigned this
    on Aug 8, 2026
  2. added
    kind/bug
    severity/s0
    on Aug 8, 2026
  3. XuPeng-SH commented on Aug 8, 2026

    @XuPeng-SH
    ContributorAuthor

    修复已提交 PR #26798:将过滤场景下严格的 ISTRUE(equal) MARK 谓词规范化为等值条件,使 NOT EXISTS 使用 Hash Anti Join;投递了 planner/compiler 回归测试。

  4. added a commit that references this issue on Aug 9, 2026
    a4b0ce2
  5. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:e8ecba8662ca4c4d9c0179978425fc08505967dc
    • 部署方式:本地 1 CN / 1 TN / 1 Log,MySQL 协议端口 6001
    • 测试日期:2026-08-12

    原问题验证

    按原问题规模构造 outer 200,000 行、inner 750,000 行(500 个匹配键,每键重复 1,500 次),原 NOT EXISTS 查询连续执行 3 次,均返回 199500,耗时约 13.8 ms / 12.3 ms / 14.7 ms。逻辑计划为 ANTI 等值连接,物理计划为 hash join + hash build,未再退化为 loop join,也未出现超时、panic 或服务重启。

    Happy Path 覆盖

    1. 小规模语义矩阵连续 3 轮,共 39/39 条断言通过;NOT EXISTS 精确结果为 3,4,8,并与 LEFT JOIN (SELECT DISTINCT ...) oracle 完全一致。
    2. 覆盖空 inner、全匹配、无匹配、复合等值、交换等值、等值加 residual 条件,结果均正确。
    3. 过滤型等值子查询保持 logical ANTI 且物理执行为 hash join;大数据规模结果与小规模语义 oracle 一致。

    Unhappy Path 覆盖

    1. outer/inner 均含 NULL:NOT EXISTS 结果正确;NOT IN 在 inner 含 NULL 时返回 0,未混淆两者语义。
    2. 投影中的 NOT EXISTS 保持 MARK join;纯非等值谓词保持 loop join,优化没有错误扩展到不支持的路径。
    3. 每轮均核对完整结果及计划关键算子;未发现缺行、重复行、错误 NULL 处理、hang 或 restart。

    白盒验证

    本轮以公开 SQL、EXPLAIN 和 PHYPPLAN 黑盒验证为主;修复 PR #26798 已包含 planner/compiler 回归。无需额外故障注入。

    回归测试

    已在 motr PR #107 增加 14_issue_regression.issue_26797_not_exists_hash_anti,覆盖 NULL/空集/复合等值/residual/NOT IN/EXISTS、MARK/非等值边界及逻辑/物理计划断言。定向 --repeat 3 为 3/3,通过;与 #26867、#26892 并行复跑总计 9/9,通过。20 万/75 万规模用于本次验证,不放入默认 motr,以避免重复构数;小用例已直接锁定本问题的算法选择。

    测试证据

    • 回归代码与稳定输出:motr PR #107
    • MatrixOne 修复:PR #26798
    • 本地保存的逐项断言:小矩阵 39/39;原规模 3/3 返回 199500,物理计划 3/3 为 hash join 且无 loop join。

    测试结论

    测试通过。

  6. added 3 commits that reference this issue on Sep 17, 2026
    4dd6e3d
    bce1d34
    821fcb8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugkind/performanceseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions