Repository navigation
Description
Problem
A correlated NOT EXISTS with JSON expressions scans and materializes every matching snapshot before applying the outer candidate keys. Explicitly restating the outer workspace and task filters inside the subquery produces the same result while avoiding almost all JSON evaluation.
Environment
- MatrixOne Docker image
matrixorigin/matrixone:4.2.5;SELECT version()returned8.0.30-MatrixOne-v4.2.5. - Standalone container, 2 CPU cores, 4 GiB memory. Synthetic data only.
- This is a local performance reproduction; behavior on current main and other releases has not been verified.
Reproduction
Execute the setup below on a disposable instance. It creates 200 tasks and 300,000 snapshots with 1 KiB JSON padding. One candidate has a matching acknowledgement; the other does not.
CREATE DATABASE pushdown_repro;
USE pushdown_repro;
CREATE TABLE digit (i INT PRIMARY KEY);
INSERT INTO digit VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
CREATE TABLE tasks (workspace_id VARCHAR(64), id VARCHAR(64), provider_run_id VARCHAR(128), state VARCHAR(32), cursor_value BIGINT, PRIMARY KEY(workspace_id,id));
CREATE TABLE snapshots (workspace_id VARCHAR(64), id VARCHAR(64), task_id VARCHAR(64), kind VARCHAR(32), committed TINYINT, snapshot_json LONGTEXT, PRIMARY KEY(workspace_id,id), INDEX idx_task(workspace_id,task_id));
INSERT INTO tasks SELECT 'ws',concat('task-',lpad(a.i+10*b.i+100*c.i,6,'0')), 'target', 'completed', 0 FROM digit a,digit b,digit c WHERE a.i+10*b.i+100*c.i<200;
INSERT INTO snapshots SELECT 'ws',concat('s-',lpad(a.i+10*b.i+100*c.i+1000*d.i+10000*e.i,6,'0')),concat('task-',lpad(a.i+10*b.i+100*c.i+1000*d.i+10000*e.i,6,'0')), 'ack',1,concat('{"provider_run_id":"other","state":"completed","event_cursor":0,"padding":"',repeat('x',1024),'"}') FROM digit a,digit b,digit c,digit d,digit e;
INSERT INTO snapshots SELECT workspace_id,concat('s2-',id),task_id,kind,committed,snapshot_json FROM snapshots WHERE id LIKE 's-%';
INSERT INTO snapshots SELECT workspace_id,concat('s3-',id),task_id,kind,committed,snapshot_json FROM snapshots WHERE id LIKE 's-%';
UPDATE snapshots SET snapshot_json='{"provider_run_id":"target","state":"completed","event_cursor":0}' WHERE workspace_id='ws' AND id='s-000000';Original query:
SELECT t.id FROM tasks t WHERE t.workspace_id='ws' AND t.id IN ('task-000000','task-000001') AND NOT EXISTS (SELECT 1 FROM snapshots s WHERE s.workspace_id=t.workspace_id AND s.task_id=t.id AND s.kind='ack' AND s.committed=1 AND JSON_UNQUOTE(JSON_EXTRACT(s.snapshot_json,'$.provider_run_id'))=t.provider_run_id AND JSON_UNQUOTE(JSON_EXTRACT(s.snapshot_json,'$.state'))=t.state AND JSON_EXTRACT(s.snapshot_json,'$.event_cursor')=t.cursor_value) ORDER BY t.id;Equivalent query with explicit subquery restrictions:
SELECT t.id FROM tasks t WHERE t.workspace_id='ws' AND t.id IN ('task-000000','task-000001') AND NOT EXISTS (SELECT 1 FROM snapshots s WHERE s.workspace_id=t.workspace_id AND s.task_id=t.id AND s.workspace_id='ws' AND s.task_id IN ('task-000000','task-000001') AND s.kind='ack' AND s.committed=1 AND JSON_UNQUOTE(JSON_EXTRACT(s.snapshot_json,'$.provider_run_id'))=t.provider_run_id AND JSON_UNQUOTE(JSON_EXTRACT(s.snapshot_json,'$.state'))=t.state AND JSON_EXTRACT(s.snapshot_json,'$.event_cursor')=t.cursor_value) ORDER BY t.id;Run both queries repeatedly, then run EXPLAIN ANALYZE for each. Both return exactly task-000001.
Observed
- Original query: 3.992 s and 4.016 s. The plan uses a RIGHT ANTI join; the snapshot scan reads 300,000 rows and its JSON Project consumes 3,367 ms on the measured run.
- Explicit restrictions: 0.011 s and 0.011 s. The plan uses an ANTI join; its snapshot scan reads 24,576 rows and passes six rows to the JSON projection.
- EXPLAIN scan metrics are per-node values; they are not a claim about physical disk bytes. Results and timings are local observations, not measurements of production.
Expected
The optimizer should investigate propagating the outer workspace and task ID candidate constraints to the inner side of the correlated anti-join, when semantics permit, so unrelated snapshot JSON need not be materialized. The explicit predicates above are a workaround for this specific query. Preserve correctness for NULLs, duplicate matches, and correlated predicates; do not assume all correlated subqueries admit the same rewrite.
Activity
MatrixAdventurer commentedon Oct 30, 2026
分级建议:s2——相关 NOT EXISTS 未传播外层候选键谓词,30 万行 JSON 全量求值(~4s,显式谓词 11ms),纯性能退化且有改写规避。模块:pkg/sql/plan(ANTI join 相关谓词拉起)。疑似重复:未发现(#26798、#28364 为相关历史实现,非同类报告)。建议负责人:XuPeng-SH(作者,#26798 即其 ANTI 优化)。本操作由LW授权的MOI智能体提交
Ariznawlll commentedon Oct 30, 2026
Main-side performance check for this report's shape (locally built
c6683bd2f5, 2026-10-30, dual-CN): the correlated NOT EXISTS + JSON-expression form does not show the "materialize-everything-before-applying-outer-keys" behavior on current main at measurable scale:Setup mirroring the report's structure (outer candidate keys + snapshots with JSON payloads, correlated on the key), scaled to 5000 work rows × 500 snapshot rows (2.5M potential probes without pushdown):
NOT EXISTS (… s.key = w.key AND JSON_EXTRACT(s.payload,'$.cfg') LIKE 'x%')All four agree on the result and run at the same single-digit-ms magnitude — the outer-key filter is being applied before JSON evaluation on main, i.e. the pushdown described as missing in 4.2.5 appears to work here. Same caveat as my note on #31619: this is a main build, not the 4.2.5 release binary from the report; providing it so triage can distinguish "fixed on main, needs cherry-pick" from "still-open, different manifestation" — reproducing the original 4.2.5 dataset shape exactly (their snapshot table's size or column types may matter, e.g. multi-MB JSON docs) would settle which.
Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。当前 main 上原查询仍对 30 万条候选行执行 JSON 投影;两种查询的结果相同,但原查询耗时明显高于限定候选键的对照。
关键证据
main@bbfe55669a1440424914d45a283a1d0e36f722fa,本地单 CN 隔离环境;使用本 issue 正文的建表、30 万行数据、原查询与限定候选键查询,仅替换数据库名。task-000001。原查询 1.570–2.062 秒(中位数 1.740 秒);对照 0.002–0.012 秒(中位数 0.0038 秒)。这是本机结果,不代表生产时延。EXPLAIN ANALYZE中,原查询的 snapshots 扫描输出 300,000 行、JSON 投影处理 300,000 行(1,647 ms);对照扫描输入 24,576 行、输出 6 行,JSON 投影处理 6 行。当前计划为 MARK join;算子行数不是物理磁盘字节数。复现 SQL 可直接使用本 issue 正文中的 SQL。待解决点
Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。扩展至 120 万行后,原相关子查询仍对全部内层行计算 JSON;查询结果正确,但候选键限制不能有效减少该计算。
关键证据
main@bbfe55669a1440424914d45a283a1d0e36f722fa,本地隔离集群,两条 CN 入口;参考本 issue 正文的原始 SQL。将其 30 万行 snapshots 用 4 个不同 ID 前缀复制到同结构、同索引的新表,得到 120 万行,替换查询中的表名,其余原查询与限定查询不变。task-000001。原查询分别为 3.045–9.285 秒、4.489–6.156 秒;限定查询分别为 0.0068–0.0522 秒、0.0057–0.0431 秒,都是本机观测,不代表生产时延。EXPLAIN ANALYZE:原查询 JSON 投影输入 1,200,000 行;限定查询投影输入 24 行。限定查询对应表扫描输入 106,496 行、输出 24 行,不能把输出行数理解为物理读取行数。待解决点
Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。扩展到 450 万行并出现实际跨 CN 执行后,原相关子查询仍对大量无关行进行 JSON 投影;所测 NULL 与重复匹配场景的结果正确。
关键证据
main@bbfe55669a1440424914d45a283a1d0e36f722fa,本地隔离双 CN。以本 issue 正文的 30 万行数据为基础,分别新建 3 张表,各将源数据复制 15 次并给 ID 加不同前缀;为控制资源,JSON 使用相同字段的约 64 字节内容。三张表是独立对象,但数据分布相同,不是三个随机数据集。EXPLAIN PHYPLAN ANALYZE显示两个 CN 的远端扫描作用域;例如第一表一条查询的汇聚算子输入 4,500,004 行,本地扫描算子合计 3,072,004 行,由汇聚与本地扫描行数对账推断另一 CN 输入 1,428,000 行。该推断依赖该计划仅有这两个扫描输入,并非直接读取到远端逐节点计数。其余 1 条按单 CN 调度。待解决点
Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。不同分布下原相关查询仍对 450 万行执行 JSON 投影;所测重复查询和异常后状态正确,但性能影响随候选键分布明显变化。
关键证据
main@bbfe55669a1440424914d45a283a1d0e36f722fa;本地隔离双 CN。参考本 issue 原始 SQL 及此前 450 万行扩展:分别构造大量任务键集中于目标键的热点数据,以及 150 万条 NULL JSON 的数据,各 450 万行。task-000001。热点数据的原查询中位数 8.161 秒、限定查询 4.719 秒;NULL 密集数据分别为 4.445 秒和 0.028 秒。数据依次运行,均为本机结果,不代表生产性能;热点数据中限定键本身命中大量行。system.statement_info的 L3 物理诊断直接记录了远端 TableScan 实例:热点数据一次执行中,6 个可见远端实例合计输入 2,454,240 行。诊断只保留部分实例,该数字是可见下限,并非远端总量。相应物理计划包含两个不同 CN 扫描作用域;完整复现背景见 此前双 CN 扩展。待解决点
Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。450 万行下候选键无命中时,原查询仍处理全部内层 JSON;候选键全命中时则不能假设显式限制一定更快。
关键证据
main@bbfe55669a1440424914d45a283a1d0e36f722fa,本地隔离双 CN。基于本 issue SQL 分别构造 450 万行:一张所有 task_id 均不匹配候选键,另一张全部命中候选键;两条 CN 入口、原查询和限定查询共 8 次,结果均符合各自预期,报错后源表统计与连接正常。max_dop=1,一次原查询的system.statement_infoL3 物理诊断直接记录两条 TableScan:本地 1,579,936 行、远端 2,920,064 行,合计 4,500,000 行,与同次逻辑扫描总数一致;调度信息为ap-multi-cn、选中 2 个节点。这是一次实际执行的完整对账,不是默认并行度或所有执行的保证。待解决点