Skip to content

[Performance]: IN on trailing composite unique-index key scans entire run history #32410

Closed

Description

@XuPeng-SH

Summary

An IN lookup over the final column of a composite unique secondary index reads an entire 5,000-row run instead of probing the three requested keys. A single equality on the same (user_id, run_id, idempotency_key) index reads one base row. This is an observed performance problem; both forms return correct results in this reproduction.

Environment

  • Disposable matrixorigin/matrixone:4.2.8, VERSION() = 8.0.30-MatrixOne-v4.2.8, GIT_VERSION() = 10d204b.
  • Synthetic data only. Row counts below are EXPLAIN ANALYZE operator counts, not throughput or latency benchmarks.

Reproduction

Run on a fresh isolated database:

CREATE DATABASE mo_in_index_repro;
USE mo_in_index_repro;
CREATE TABLE digits (n INT PRIMARY KEY);
INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
CREATE TABLE events (
  id VARCHAR(64) NOT NULL, user_id VARCHAR(128) NOT NULL,
  run_id VARCHAR(64) NOT NULL, session_id VARCHAR(64) NOT NULL,
  event_idx BIGINT NOT NULL, event_type VARCHAR(64) NOT NULL,
  idempotency_key VARCHAR(128), payload_json TEXT NOT NULL,
  PRIMARY KEY (user_id,id),
  UNIQUE KEY uq_user_run_key (user_id,run_id,idempotency_key),
  KEY idx_session (user_id,session_id,run_id,event_idx),
  KEY idx_type (user_id,run_id,event_type,event_idx)
);
INSERT INTO events
SELECT CONCAT('id',a.n+b.n*10+c.n*100+d.n*1000),
       'target','long-run','session',a.n+b.n*10+c.n*100+d.n*1000,'tool_result',
       CASE a.n+b.n*10+c.n*100+d.n*1000
         WHEN 1 THEN 'user_intent:x'
         WHEN 2 THEN 'user_intent_applied:x'
         WHEN 3 THEN 'user_intent_returned:x'
         ELSE CONCAT('event:',a.n+b.n*10+c.n*100+d.n*1000) END,
       '{}'
FROM digits a CROSS JOIN digits b CROSS JOIN digits c CROSS JOIN digits d
WHERE a.n+b.n*10+c.n*100+d.n*1000 < 5000;

EXPLAIN ANALYZE SELECT payload_json,event_idx,session_id FROM events
WHERE user_id='target' AND run_id='long-run'
  AND idempotency_key='user_intent:x';

EXPLAIN ANALYZE SELECT payload_json,event_idx,session_id FROM events
WHERE user_id='target' AND run_id='long-run' AND session_id='session'
  AND idempotency_key IN ('user_intent:x','user_intent_applied:x','user_intent_returned:x');

Observed and expected

  • Equality selected uq_user_run_key: base table inputRows=1, outputRows=1.
  • IN selected a table scan: inputRows=5000, outputRows=3.
  • UNION ALL of three equality point lookups selected uq_user_run_key in all three branches and returned the same three keys. On the larger application-shaped fixture, including the extra session_id predicate in each branch sometimes selected the less selective session index, so the application checks the returned session after the unique-key lookups before exposing any data.

Expected: a bounded set of exact keys on the fully specified composite unique index should use bounded point lookups, without work proportional to run history. FORCE INDEX did not repair the larger fixture. This is related to #32370, but here the leading composite key columns are fixed and the IN values are on the final unique-key column, within one user/run.

Astra context: matrixorigin/Astra#1243.

Activity

  1. added
    severity/s0
    ai-easy
    and removed on Nov 10, 2026
  2. gouhongshen commented on Nov 11, 2026

    @gouhongshen
    Contributor

    PR #32577 is trying to fix this issue.

  3. gouhongshen commented on Nov 16, 2026

    @gouhongshen
    Contributor

    PR #32577 has merged and is awaiting manual validation. The issue remains open.

  4. gouhongshen commented on Nov 16, 2026

    @gouhongshen
    Contributor

    PR #32830 is trying to fix this issue.

  5. gouhongshen commented on Nov 17, 2026

    @gouhongshen
    Contributor

    PR #32830 has merged and is awaiting manual validation. The issue remains open.

  6. XuPeng-SH commented on Nov 17, 2026

    @XuPeng-SH
    ContributorAuthor

    转 QA 验证,不代表验收通过,保持 issue 开放。

    #32830 已于 2026-11-17 合入 main:a7e743753131ba9867e0f7b4f5722f6c7e84afcc(head 731df583b6176893f31e469a45df756beb617207),将 #32577 / #32567 的相关支持适配到 main。完整 full-value composite UNIQUE key 的其他部分等值、一个有限非 NULL 常量 IN 维度可生成精确去重 key set;候选上限为 128 points、1 MiB planning work、8 MiB wire data,拒绝准入时保留普通访问 fallback。

    验证证据按来源区分:

    • 作者报告 5,000 行原始形状普通/forced 查询都选 uq_user_run_key,索引和 base table inputRows=3 / outputRows=3,返回 event_idx 1/2/3;残余过滤、非覆盖投影与 backfill 保留。还报告完整 planner、编码对比与 56/56 real SQL BVT,旧 RED 基于 b815a4c,并非最终 merge-base。
    • 后续评审在 56ee1cf 上独立运行 focused 16 roots、完整 planner 5,076 roots(两个既有无关 subtest skip),以及单 CN 公共 MySQL 协议 SQL probe 同进程两轮:每轮 56 原始语句、12 plan/work assertions、23 独立结果集,含 prepared reuse 和 teardown。
    • 该 probe 不验证 mo-tester metadata/format rendering;multi-CN 成功仅来自 supplied CI,旧作者原始运行日志和外部 reusable workflow bodies 未提供。早期 bb3ee9b 评审没有本地执行,不与后续独立执行混淆。最终队列因 fork 编辑权限出队,合并由 metadata 确认,不能由此推断最终 CI 全绿。

    inputRows 证明该 fixture 工作量有界,不是延迟或吞吐 benchmark。动态值、含 NULL、多 IN 维度、declared prefix、错误 metadata、超资源限制等采用保守 fallback,并非承诺所有 IN 均走 key-set;仍须保持 SQL 语义。本轮只核对远端证据,没有在最终合并 SHA 自行实测。PR 所列 #32745 teardown/heartbeat flaky 与本修复独立,本 PR 没有修复它,不据此关闭该 issue。

    请 QA 在最终 main 重跑本 issue 5,000 行原始查询与 equality 对照,核对结果、索引选择和实际 input/output rows;覆盖重复/不存在 key、NULL 三值逻辑、prepared reuse、残余与 backfill、index-only、竞争索引/缺统计、hint/prefix 安全,及 128 点、字节/预算边界、取消、重试和 fallback。补多 CN 与实际规模的延迟/吞吐验证,但未测性能不作为阻转条件。不自动要求或执行 backport。

    已读完整 issue/PR 评论和评审,未见当前适用的已验证反例或未完开发 follow-up。按持续交接流程移除原研发负责人,改派唯一 @Ariznawlll。

  7. slyunnn commented on Nov 19, 2026

    @slyunnn

    Regression and extended verification for #32410 (2026-11-19)

    Verification Environment

    • main: latest fetched ae9e665f3093094ba3286e72bf1af8e111463180, perf(exec): reduce accounted work on short index range scans (#32895); fix fix: use bounded composite unique index key sets #32830, merge a7e743753131ba9867e0f7b4f5722f6c7e84afcc.
    • 4.2-dev: latest fetched 71e22339c8cca62b2f29f9fd6e686e653a1ff2d2, [4.2-dev] fix(frontend): preserve string literals in merged role rules (#32891); fix plan: bound composite unique index trailing IN lookups #32577, merge dec4ce9772e281203d70c390a74bce0937c39ee6.
    • Each branch used its own matching binary and isolated 2 CN / 1 TN / 1 Log / Proxy cluster. Proxy 6001, CN1 16001 and CN2 16002 were reachable. The original SQL and extended product test ran on every endpoint over JDBC/MySQL protocol. Branches and cases were executed serially; no engine-parallel claim is made.

    What Was Covered

    • OK: The original 5,000-row fixture and equality control, actual EXPLAIN ANALYZE plan assertions and result checks on each endpoint. The unique index was selected; the original three-key IN query's base scan reported inputRows=3, outputRows=3, while equality reported 1/1.
    • OK: Existing optimizer/composite_unique_in_index.sql: main 56/56 and 4.2-dev 55/55 on each of Proxy, CN1 and CN2, with no failed or ignored statements. It covers duplicate/absent keys, NULL semantics, prepared reuse, residual predicates and base-row backfill, forced index, prefix indexes and competing/missing-statistics controls.
    • OK: Focused TestCompositePointCandidate and TestCompositeUnique* planner tests passed on both branches, including bounded point counts, encoded-byte budget and fallback. These are planner tests, not live latency benchmarks.
    • Not covered: Customer-scale latency, throughput, cancellation or sustained concurrent workload. The 5,000-row fixture is required to show bounded scan work for the reported regression; operator row counts do not establish a production performance SLA.

    Findings

    The original query returned the expected keys and used bounded access on both maintenance branches at the tested commits.

    MOTR / BVT Coverage

    Existing product BVT directly covers the original SQL, plan and result boundaries on both branches, so an additional duplicate MOTR case is unnecessary. No Nightly performance workflow was added.

    Final Conclusion

    Fixed on the tested main and 4.2-dev revisions for the reported bounded-scan regression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easyseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions