Repository navigation
Description
Description
Projected correlated EXISTS with nullable composite correlation values is dramatically slower on a duplicate-heavy witness table. Adding a semantically equivalent DISTINCT to the witness columns preserves all 288 result rows (including the outer NULL cases) and reduces local runtime from 12–20 seconds to 0.09–0.32 seconds on three independently generated datasets. This is a performance finding, not a wrong-result claim.
Environment
- Branch and runtime:
main@bbfe55669a1440424914d45a283a1d0e36f722fa - Isolated local 2 CN / 1 TN on macOS. The measured SQL uses one CN entry; this is not a production SLA or historical regression measurement.
Steps to reproduce
Run the following Python with an already configured PyMySQL connection con to an isolated database. rep can be 0, 1, or 2 to obtain independent distributions.
import random
con.autocommit(True)
rep = 0
rng = random.Random(1939 + rep * 631)
outer = [(t,i,None if i%7 == 0 else i%17,
None if i%9 == 0 else i%11)
for t in range(1,4) for i in range(1,97)]
inner = []
for i in range(1,24001):
t = rng.randrange(1,7)
k = (None if i % (3 if rep == 1 else 13) == 0 else
(1 if rep == 2 and i % 2 == 0 else rng.randrange(28)))
v = None if i % 5 == 0 else rng.randrange(11)
inner.append((t,i,k,v,0 if i % 3 == 0 else 1))
inner += [(1,24001,16,None,1),(1,24002,16,None,1),
(1,24003,16,4,1),(1,24004,16,4,1)]
with con.cursor() as c:
c.execute('CREATE TABLE tasks(tenant INT,id INT,k INT,v INT,PRIMARY KEY(tenant,id))')
c.execute('CREATE TABLE seed(tenant INT,id INT,k INT,v INT,flag INT,PRIMARY KEY(tenant,id))')
c.execute('CREATE TABLE copies(n INT PRIMARY KEY)')
c.execute('CREATE TABLE snaps(tenant INT,id BIGINT,k INT,v INT,flag INT,PRIMARY KEY(tenant,id),INDEX ix_key(tenant,k,v))')
c.executemany('INSERT INTO tasks VALUES(%s,%s,%s,%s)', outer)
c.executemany('INSERT INTO seed VALUES(%s,%s,%s,%s,%s)', inner)
c.executemany('INSERT INTO copies VALUES(%s)', [(i,) for i in range(188)])
c.execute('INSERT INTO snaps SELECT s.tenant,s.id+25000*c.n,s.k,s.v,s.flag FROM seed s CROSS JOIN copies c')Original:
SELECT t.tenant,t.id,
EXISTS(SELECT 1 FROM snaps s WHERE s.tenant=t.tenant
AND s.k=t.k AND s.flag=1 AND s.v=t.v) AS present
FROM tasks t ORDER BY t.tenant,t.id;Equivalent control (duplicates of these complete witness tuples cannot change EXISTS):
WITH compact_snap AS (SELECT DISTINCT tenant,k,v,flag FROM snaps)
SELECT t.tenant,t.id,
EXISTS(SELECT 1 FROM compact_snap s WHERE s.tenant=t.tenant
AND s.k=t.k AND s.flag=1 AND s.v=t.v) AS present
FROM tasks t ORDER BY t.tenant,t.id;Actual behavior
Each table contains 4,512,752 witness rows. After setup, the control was run immediately before and after the original on each separate dataset; all complete results were compared against an independent oracle.
| Seed | Control before | Original | Control after |
|---|---|---|---|
| 0 | 0.323 s | 19.778 s | 0.131 s |
| 1 | 0.106 s | 12.398 s | 0.088 s |
| 2 | 0.088 s | 20.063 s | 0.096 s |
A larger four-marker variant containing projected EXISTS, NOT EXISTS, IN and NOT IN was also run 3 times per CN entry on each dataset: all 18 results matched, original 47.6–86.1 s. Its DISTINCT control returned the same rows; 20 repeated controls on seed 0 took 0.36–1.29 s. These observations include local cache and scheduling effects, and some diagnostic work ran concurrently.
Expected behavior
Evaluate whether duplicate-heavy nullable witness inputs can use a bounded, semantics-preserving representation or another efficient plan, without requiring application SQL to pre-deduplicate. Preserve exact TRUE/FALSE/UNKNOWN, empty input, duplicate outer rows and errors. Do not replace nullable composite MARK joins with an ordinary hash join without a correctness proof.
Stability and controls
- Three distinct seeded datasets reproduce the difference with controls bracketing the original; all 288 rows match an independent Python oracle.
- NULL-heavy and skewed distributions are included; small MySQL 8.3 comparisons, duplicate matches, empty input, and invalid-column/unchanged-source controls also pass.
- One 4-marker
EXPLAIN ANALYZEshows each original MARK join receiving 3,009,040 rows and recording 23–30 s; the DISTINCT control feeds each 2,084 rows after scanning/aggregating the same table. Operator times are not additive wall-clock times. - A small-fixture physical
EXPLAIN PHYPLAN ANALYZEshows loop join. The large physical-plan collection timed out on the client after 90 s; service remained available, so the exact large physical operator is not asserted.
Code analysis
canUseHashMarkJoinWithInputs intentionally excludes unsafe nullable composite-key shapes to preserve three-valued SQL semantics; compile.go chooses the loop fallback when the proof fails. The small physical plan confirms loop execution; the large-plan root cause remains a hypothesis.
Regression coverage
Bounded work/performance tests should compare duplicate-heavy, unique and empty witnesses; NULL and non-NULL composite keys; single and repeated projected predicates; and distributed execution. Independently assert exact results, NULL state and outer-row multiplicity. Include unfavorable high-distinct-cardinality cases before choosing any automatic deduplication.
Related
- [Bug]: IN/NOT IN MARK joins fall back to nested loops and large expressions are left-deep #25821 (closed) deliberately retains row-aware evaluation for unsafe nullable composite keys; this report concerns the observed duplicate-heavy performance opportunity, not a request to bypass that safety condition.
- [Subtask]: Share conditional witness summaries for paired EXISTS and NOT EXISTS #31139 considers sharing witness summaries for paired semi/anti predicates; this report covers a projected MARK result with nullable correlations and supplies a measured workload.
- [Performance]: correlated NOT EXISTS evaluates unrelated JSON rows before applying outer key filters #31769 covers candidate-key propagation before JSON evaluation; this reproduction has no JSON or selective outer key filter.
Activity
perf(sql): hash safe nullable composite EXISTS witnesses (#31848)
XuPeng-SH commentedon Oct 30, 2026
转 QA 验证:#31848 已合入 main,合并提交
4183159a877e27049a392906ea2c3ae07edcdd42。编译保留 Boolean EXISTS 证明,在匹配整数复合键、无残余/计算键且外表估计大于 1 的广播 MARK 路径复用 hash membership;不改普通 IN/NOT IN 的三值语义。
本轮证据为 issue 全文/完整评论与合并源码、测试内容静态核对,未运行本地 MatrixOne 实例或回归;PR 作者自述的运行结果不算本轮独立测试。当前未找到适用于新修复的已验证反例或仍未完成的开发 follow-up,按转测口径交给 Ariznawlll;这不是验收通过。
请 QA 继续覆盖:原重复重度/NULL/高基数数据集、精确全行和 NULL 状态、空输入与重复外行、双 CN、预算拒绝清理、单探针旧路径;内存换时延和不支持域不宣称优化。
Ariznawlll commentedon Oct 31, 2026
结论
测试通过。原重复密集的 nullable EXISTS 场景在本地原规模下结果正确,符合本次修复的验收范围。
关键证据
main@1565fb79ab3a230f0e383300b5be47a4b7ae6cda;本地隔离 2 CN / 1 TN / 1 LogService,运行二进制完整 SHA 一致,vcs.modified=false。修复 perf(sql): hash safe nullable composite EXISTS witnesses #31848 的合并提交4183159a877e27049a392906ea2c3ae07edcdd42在该版本祖先链。EXPLAIN PHYPLAN ANALYZE显示该场景 hash join,单个探测行的对照显示 loop join;不宣称对 residual/computed key 等修复未支持的域均有优化。待解决点