Repository navigation
Description
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 keycustomer_idfact_orders: 750,000 rowsfact_orders.campaign_id: 681,819 non-NULL values, 500 distinct values, plus NULLsfact_ordershas 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.
EXPLAINshows anANTIjoin 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
XuPeng-SH commentedon Aug 8, 2026
修复已提交 PR #26798:将过滤场景下严格的
ISTRUE(equal)MARK 谓词规范化为等值条件,使 NOT EXISTS 使用 Hash Anti Join;投递了 planner/compiler 回归测试。fix(plan): use hash anti join for filtered NOT EXISTS (#26798)
Ariznawlll commentedon Aug 12, 2026
测试版本
maine8ecba8662ca4c4d9c0179978425fc08505967dc原问题验证
按原问题规模构造 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 覆盖
3,4,8,并与LEFT JOIN (SELECT DISTINCT ...)oracle 完全一致。Unhappy Path 覆盖
NOT IN在 inner 含 NULL 时返回 0,未混淆两者语义。白盒验证
本轮以公开 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,以避免重复构数;小用例已直接锁定本问题的算法选择。测试证据
199500,物理计划 3/3 为 hash join 且无 loop join。测试结论
测试通过。
fix(plan): use hash anti join for filtered NOT EXISTS (matrixorigin#2…
fix(plan): use hash anti join for filtered NOT EXISTS (matrixorigin#2…
fix(plan): use hash anti join for filtered NOT EXISTS (matrixorigin#2…