Skip to content

[Performance]: OR of composite primary-key points scans the full tenant prefix #32365

Closed

Description

@XuPeng-SH

Summary

On MatrixOne v4.2.8, a prepared disjunction of two complete points in the same composite primary-key prefix scans the entire tenant prefix. Factoring the shared prefix and using IN returns the same two rows while reading only those two rows. A real Astra batch UPDATE with 50 such identities likewise scans 100,000 rows to update 50.

This is a performance/access-path observation. Both SELECT forms returned the correct rows; no wrong-result behavior was observed. It is distinct from #31863, which concerns OR across different secondary indexes, and #31046, which concerns selection of a secondary index in the presence of a primary-key prefix.

Environment

  • Disposable matrixorigin/matrixone:4.2.8 container, no production data.
  • SELECT VERSION(), GIT_VERSION(), BUILD_VERSION() returned 8.0.30-MatrixOne-v4.2.8, 10d204b, 2026-11-04 08:32:41.
  • Server-side SQL PREPARE/EXECUTE; not yet tested against a newer main build or the binary prepared-statement protocol.

Reproduction

CREATE DATABASE cpkey_or_repro;
USE cpkey_or_repro;
CREATE TABLE digits (n INT);
INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
CREATE TABLE t (
    tenant VARCHAR(16) NOT NULL,
    session_id VARCHAR(16) NOT NULL,
    request_id VARCHAR(16) NOT NULL,
    result VARCHAR(16),
    PRIMARY KEY (tenant, session_id, request_id)
);
INSERT INTO t
SELECT 'user', 'session',
       CONCAT('q', LPAD(a.n + b.n*10 + c.n*100 + d.n*1000 + e.n*10000, 6, '0')),
       NULL
FROM digits a CROSS JOIN digits b CROSS JOIN digits c
     CROSS JOIN digits d CROSS JOIN digits e;

PREPARE q1 FROM 'EXPLAIN ANALYZE SELECT result FROM t WHERE tenant=? AND ((session_id=? AND request_id=?) OR (session_id=? AND request_id=?))';
SET @p0='user', @p1='session', @p2='q000001', @p3='session', @p4='q000002';
EXECUTE q1 USING @p0,@p1,@p2,@p3,@p4;
DEALLOCATE PREPARE q1;

PREPARE q2 FROM 'EXPLAIN ANALYZE SELECT result FROM t WHERE tenant=? AND session_id=? AND request_id IN (?,?)';
SET @p0='user', @p1='session', @p2='q000001', @p3='q000002';
EXECUTE q2 USING @p0,@p1,@p2,@p3;
DEALLOCATE PREPARE q2;

Observed

Form Base-table scan inputRows outputRows
tenant=? AND ((session=? AND request=?) OR (session=? AND request=?)) 100,000 2
tenant=? AND session=? AND request IN (?,?) 2 2

The first plan uses prefix_eq for the tenant composite-key prefix and applies both session/request branches as residual predicates. The second plan uses composite-key point filters. Factoring the duplicated session predicate is an observed workaround when all requested identities share a session; batches spanning different sessions need a more general strategy.

As a product-shaped control, a 50-identity server-side prepared EXPLAIN ANALYZE UPDATE using the Astra edge_pending_dispatch schema, its secondary index, and the current CASE/OR condition read 100,000 base-table rows to update 50. An equivalent factored single-session update read 50. These are isolated plan observations, not production latency or throughput measurements.

Expected

For multiple equality points on the same composite primary key, preserve selective point access or otherwise avoid reading the entire leading-key prefix when the repeated predicates can be factored. Prepared and literal forms should preserve results and efficient access.

Activity

  1. self-assigned this
    on Nov 8, 2026
  2. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    ContributorAuthor

    QA handoff for this issue #32365: related fix PR #32377 merged into main at 2026-11-09 00:10:48 UTC as 93a17bb. It derives bounded correlated complete composite-primary-key points while retaining the original predicates, with access-rewrite revalidation, query-wide admission limits and prepared-plan value-dependence handling. No current applicable counterexample or unfinished development follow-up was found in the issue's complete discussion and the final PR review. This is a handoff for QA validation, not an acceptance pass.

    Evidence: the final reviewer reports full planner/EXPLAIN/readutil normal tests, fourteen focused tests and public binary-prepared SELECT/UPDATE tests passed; the earlier design-review gap was resolved. Its full frontend run failed at unchanged Test_openNewFile_nilCtx due to a read-only-checkout fixture, while affected frontend tests passed. The author separately reports planner normal/race, focused frontend race coverage, two rounds each of 64/64 composite-point and 56/56 block-filter BVT, and 299/299 prepared numeric/warning BVT. These are attributed remote reports; I ran no local product tests or benchmarks in this pass.

    QA boundaries: reproduce the issue's two-point and fifty-identity SELECT/UPDATE cases on the merged revision using literal, SQL PREPARE and binary prepared protocols; verify exact results and affected rows, scan input, multi-session identities, NULL/type rebinding, cache reuse, warnings/errors and rollback. Exercise admission-limit and unsupported-shape fallback without losing predicates. The author reports 100,001 to 50 scan rows and lower local latency, but planning cost increases and blocks touched were 14 versus 13; these measurements do not establish proportional I/O savings, production latency bounds or multi-CN performance. Related 4.2-dev PR #32371 is separate and currently unmerged; this handoff adds no backport requirement.

    Assigning to Ariznawlll for QA, replacing the development assignee.

  3. added a commit that references this issue on Nov 9, 2026
    fbbfab7
  4. MatrixAdventurer commented on Nov 9, 2026

    @MatrixAdventurer
    Contributor

    【例行核实】结论:已修复可关闭(main 与 4.2-dev 均已合入)。证据:#32377 已合入 main(11-09 00:10,93a17bb),#32371 已合入 4.2-dev(11-09 00:18):按共享前缀+相关完整复合主键点推导有界点集、保留原谓词并做查询级预算与 join 后复验(pkg/sql/plan/expr_opt.go、opt_misc.go、query_builder.go),readutil/pk_filter.go 仅当点集全落前缀才优先精确点。回归测试:expr_opt_test.go 反例集、TestCompositePointBinaryPrepareReuse(两点/50点 scan inputRows 断言与 rebind)、新 BVT composite_key_points.sql(unicode/尾空格/left join/prepare 重绑);隔离实例 BVT 与 -race 为评审转述,未本地复跑。还缺:合并 run 的 CI Required 有失败(归因无关 fixture)、QA 现网复测。@Ariznawlll 请确认后关闭。本操作由LW授权的MOI智能体提交

  5. Ariznawlll commented on Nov 9, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。原 issue 中“相同 session 的两个复合主键点 OR 导致 100,000 行输入”的计划在当前 main 未复现;结果集一致且两种计划均只处理 2 行。

    关键证据

    • 版本:main@c99234dcf4c94d1ff9d372917cd35041f1a3a682,隔离本地 CN,运行时 git_version() 为 c99234dcf4。
    • 3 个各含 100,000 行的独立表上,tenant='u' AND ((session_id='s' AND request_id='q000001') OR (session_id='s' AND request_id='q000002')) 与等价 tenant='u' AND session_id='s' AND request_id IN (...) 均返回 (1),(2);EXPLAIN ANALYZE 两者均为 inputRows=2。
    • OR 计划 ReadSize=155.21 KiB,IN 计划 113.00 KiB;仍有读取字节差异,但没有复现全前缀 100,000 行扫描。另一个含不同 session 的额外 OR 分支会扫描 100,000 行,不属于该 issue 的原始同 session 见证。

    待解决点

    • 只验证同 session 的两点 SELECT;未验证不同 session 的真实应用 UPDATE 批次或更大表规模。
  6. Ariznawlll commented on Nov 9, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。原 Issue 的 10 万行复合主键前缀扫描问题,在本次验证的 main 和 4.2-dev 上未复现。

    关键证据

    • 版本:本地 main@e2459cc55f4b514893e19732743460bc5f16d552,以及隔离 Linux 环境 4.2-dev@a860d63a97cdc38eee90cdbea2e5b351f716328c;均核对运行时 git_version()。
    • 黑盒 Happy Path:在 100,000 行表上,SQL PREPARE 两点 OR 和等价 IN 查询均返回正确的 2 行;main 的 EXPLAIN ANALYZE 显示两种形式均为 inputRows=2。50 点更新影响 50 行、回滚后无残留;相关场景重复 3 次。
    • 黑盒 Unhappy/边界:main 上验证 NULL、缺失及重复点、参数重绑和不支持形态回退;binary prepared 的 50 个跨 session 点扫描 50 行并更新、回滚正确。128 点扫描 128 行;129 点回退扫描 100,025 行,仍返回准确的 129 行。双 CN 分别验证了两点查询和 50 点更新回滚。
    • 白盒与回归:main 的复合点 BVT 为 64/64,重复 3 次;MOTR PR #314 覆盖两分支,main 和 4.2-dev 的针对性用例均通过 3/3。两分支使用同一用例和分支基线。

    待解决点

    • MOTR PR 尚未合入;4.2-dev 每日任务已配置包含该套件,需在合入后的定时运行中确认实际执行。
    • 本地完整 14_issue_regression suite 未完成:既有 [Bug]: JSON window partitions split equal numeric values and make correlated LIMIT 1 return multiple rows #31636 用例后进程组清理未确认,保护机制阻止了后续用例;另外 3 个既有用例报告失败。全新隔离环境的 32xxx 子集中,本 Issue 回归通过。
    • 原报告的发行版为 4.2.8;本次验证的是较新的 4.2-dev 提交,不代表已发布的 4.2.8 镜像行为发生变化。未将本次扫描行数结果解释为生产环境吞吐量保证。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions