Skip to content

[Performance]: projected nullable EXISTS spends seconds on duplicate-heavy MARK join witnesses #31829

Closed

Description

@Ariznawlll

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 ANALYZE shows 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 ANALYZE shows 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

Activity

  1. added this to the MO-202610 milestone on Oct 30, 2026
  2. added
    kind/bug
    on Oct 30, 2026
  3. added theissue type on Oct 30, 2026
  4. added a commit that references this issue on Oct 30, 2026
    4183159
  5. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    转 QA 验证:#31848 已合入 main,合并提交 4183159a877e27049a392906ea2c3ae07edcdd42。

    编译保留 Boolean EXISTS 证明,在匹配整数复合键、无残余/计算键且外表估计大于 1 的广播 MARK 路径复用 hash membership;不改普通 IN/NOT IN 的三值语义。

    本轮证据为 issue 全文/完整评论与合并源码、测试内容静态核对,未运行本地 MatrixOne 实例或回归;PR 作者自述的运行结果不算本轮独立测试。当前未找到适用于新修复的已验证反例或仍未完成的开发 follow-up,按转测口径交给 Ariznawlll;这不是验收通过。

    请 QA 继续覆盖:原重复重度/NULL/高基数数据集、精确全行和 NULL 状态、空输入与重复外行、双 CN、预算拒绝清理、单探针旧路径;内存换时延和不支持域不宣称优化。

  6. Ariznawlll commented on Oct 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。原重复密集的 nullable EXISTS 场景在本地原规模下结果正确,符合本次修复的验收范围。

    关键证据

    • 官方 main@1565fb79ab3a230f0e383300b5be47a4b7ae6cda;本地隔离 2 CN / 1 TN / 1 LogService,运行二进制完整 SHA 一致,vcs.modified=false。修复 perf(sql): hash safe nullable composite EXISTS witnesses #31848 的合并提交 4183159a877e27049a392906ea2c3ae07edcdd42 在该版本祖先链。
    • 按原场景构造 288 个外行、24,004 个种子、188 份副本,生成 4,512,752 条内行;使用 3 个确定性随机种子,两个 CN 分别执行原 correlated EXISTS,并在前后执行 DISTINCT 对照。每次全部 288 行都与由种子数据独立计算的 Python 结果逐行一致(每 CN 3/3),覆盖大量重复、NULL 和重复外部键。原查询本轮耗时约 0.23–0.84 秒;不同版本/环境未做受控交错 A/B,不能将该数字当成生产性能阈值或严格加速比。
    • 额外以 262,144 个不同构建键、288 个探测行检查高基数及 NULL,两个 CN 原查询和 DISTINCT 对照各重复 3 次,均与独立结果一致;空输入、错误列引用、普通 IN/NOT IN 的三值逻辑及 binary prepared 参数复用也通过。实际 EXPLAIN PHYPLAN ANALYZE 显示该场景 hash join,单个探测行的对照显示 loop join;不宣称对 residual/computed key 等修复未支持的域均有优化。
    • 当前源码中 Boolean EXISTS MARK 的编译选择、NULL 掩码和重用、默认 false、producer proof 定向 Go 测试均重复 3 次通过。预算拒绝额外使用隔离源码的临时测试变体:将原有广播预算测试改成 composite Boolean EXISTS MARK,保留 4,096 字节预算、双消费者、OOM 传播与资源归零断言,重复 3 次通过;这属于内部注入测试,不等同真实 SQL 预算耗尽。
    • 轻量的公开 SQL 语义回归已加入 MOTR #298,在本地两个 CN 运行通过;原规模、高基数及预算测试的复现记录 独立保存,不在普通 MOTR 运行中制造大负载。新增用例的 离线 CI 四项通过。

    待解决点

    • 本轮是本地隔离拓扑,不覆盖生产负载或长期资源趋势;预算拒绝是定向 Go 注入而非端到端 SQL 故障注入,轻量 MOTR 也不替代原规模性能测试。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions