Skip to content

[Performance]: 4.2-dev chooses a broad composite index over a one-row task lookup for ordered LIMIT 1 #33088

Done

Description

@iamlinjunhong

Is there an existing issue for performance?

  • I have checked the existing issues.

Environment

  • MatrixOne 4.2-dev at f811a8673b44f592e5f60db10843de119d662e89, verified with git_version(). Also reproduced at 58edabfe8f9f.
  • Local Apple M4, macOS 15.5 arm64, one CN, one TN. Synthetic data, one client.

Details of Performance

A point lookup with three equality predicates and ORDER BY created_at, id LIMIT 1 chooses the broad (workspace_id, workflow_id) secondary index for a task that has exactly one row. The existing nonunique (task_id) index gives the same result with much less work. This happens after ANALYZE TABLE and without forcing an index in the original query.

Run the following in a fresh local database:

CREATE DATABASE top1_scattered;
USE top1_scattered;
CREATE TABLE runs (
  id BIGINT PRIMARY KEY,
  workspace_id INT NOT NULL,
  workflow_id INT NOT NULL,
  task_id INT NOT NULL,
  created_at BIGINT NOT NULL,
  body VARCHAR(256) NOT NULL,
  INDEX ix_flow (workspace_id, workflow_id),
  INDEX ix_task (task_id)
);
INSERT INTO runs
SELECT result,
       CASE WHEN MOD(result * 79,150000) < 110469 THEN 1 ELSE result END,
       CASE WHEN MOD(result * 79,150000) < 110469 THEN 1 ELSE result END,
       CASE WHEN MOD(result * 79,150000) < 15665 THEN 7 ELSE MOD(result * 79,150000) + 10 END,
       MOD(result * 173, 200003), REPEAT('x', 200)
FROM generate_series(1,150000) g;
ANALYZE TABLE runs;

-- Unhinted query
EXPLAIN ANALYZE SELECT * FROM runs
WHERE workspace_id = 1 AND workflow_id = 1 AND task_id = 100010
ORDER BY created_at, id LIMIT 1;

-- Same data, filters, projection and ordering. Exclude only the broad index.
EXPLAIN ANALYZE SELECT * FROM runs IGNORE INDEX (ix_flow)
WHERE workspace_id = 1 AND workflow_id = 1 AND task_id = 100010
ORDER BY created_at, id LIMIT 1;

Run the two SELECTs without EXPLAIN to verify the result. Both return ID 100000. Of 150,000 rows, 110,469 match the workspace/workflow pair and exactly one matches this task. The two leading filter columns are perfectly correlated in this fixture.

Measured on f811a8673b44 for the one-row task:

Plan Index scan input/output rows Base scan input/output rows Warm SELECT median
Automatic, ix_flow 49,152 / 49,152 150,000 / 1 1.84 ms
Exclude ix_flow, selects ix_task 1 / 1 1 / 1 0.36 ms

Scan counts are operator counters from EXPLAIN ANALYZE. The 110,469 matching rows describe the data distribution. Timings are client wall time, median of six timed samples per path after warm-up in alternating order. The work difference is the primary observation; millisecond timings depend on caching and hardware.

Expected: consider the task-key access cost when competing nonunique equality paths have very different fanout. Keeping the output row, projection and ordering intact, the existing task path shows that a base scan over all 150,000 rows is avoidable for this literal.

Additional information

  • Dense-key control: replace 100010 with 7. The task has 15,665 matches and both paths return ID 26590. We did not observe a consistent latency advantage for the task path across both builds. The requested improvement is specific to plan quality under skew.
  • Adding an index on (workspace_id,workflow_id,task_id,created_at,id) is unnecessary to demonstrate the rare-key issue. In a separate dense-key control on the newer SHA that index was chosen and the plan retained a Sort.
  • Related: [Performance]: composite index backfill defeats ordered Top-N pruning for dense keys #31832 compares secondary-index backfill with primary-key Top-N for a dense key; [Performance]: FORCE INDEX with unrelated ORDER BY drops selective index bounds #31858 concerns an explicit FORCE INDEX losing bounds. This reproduction compares two existing secondary point paths under automatic selection. The precise estimator decision and behavior on main have not been established.

Activity

  1. added
    severity/s0
    on Nov 23, 2026
  2. XuPeng-SH commented on Nov 23, 2026

    @XuPeng-SH
    Contributor

    转测:#33094 已合入 4.2-dev(b20154b6ee4489f43650fa980e9d0d40e5b7e6c5),合入树与最终 PR head feba5c1d 相同。请 @Ariznawlll 验证原 4.2-dev 的数据倾斜场景。本次是转测,不是验收;不扩展到 main 的相邻用例或任何生产发布。

    PR 报告在较早 head e5ab4497 上用原 15 万行样本,ANALYZE TABLE 后首次无提示查询自动选窄索引,索引与主表各读 1 行,六列结果正确;密集键和反向倾斜保留对照。该证据不是最终合入树的独立实测。最终 CI Required 未通过,Linux UT、覆盖率产物、SCA 和两项多 CN BVT 均被取消;不将取消视为产品失败或替代原场景复验。

    请对照原查询与排除宽索引的同结果控制,核 EXPLAIN ANALYZE 实际扫描量、投影与 ORDER BY/LIMIT;补验首次查询前有无诊断 COUNT、密集键/反向倾斜、冷/部分缓存及不确定估计的回退,并核实采样没有新增存储读取。不要把扫描行数对照当延迟 SLA。工单保持开放。

  3. Ariznawlll commented on Nov 24, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。原工单 15 万行热缓存倾斜场景已改善;本次增加的 150 万行规模下,三轮无提示查询仍选择宽索引并扫描整张主表,尚不能完成大数据性能验收。修复 PR 明确允许采样不充分时回退原排序,目前未证明回退原因或判定这是独立代码缺陷,不改派。

    关键证据

    • 环境:官方 4.2-dev@5285c9fc52eb4c1a273854bb5fe93c64a2a2ad07,本地 macOS arm64 隔离单机(1 CN / 1 TN / 1 LogService);SQL git_version() 与构建源码 SHA 一致。修复 #33094 的合并提交已包含在此版本。
    • 原始 15 万行:三个独立数据集,分别先执行工单无提示 EXPLAIN ANALYZE,之后才做分布 COUNT;稀疏键 task_id=100010 自动选 ix_task,索引与主表各 input/output=1/1,结果六列与排除 ix_flow 对照一致、id=100000。密集键 id=26590;反向倾斜三个独立数据集自动选 ix_flow,索引和主表各读 1 行,id=100000。
    • 重启实例后的原始数据集第一次查询回退至 ix_flow,索引读 49,152 行、主表读 150,000 行,结果正确;随后连续 10 次选 ix_task。进程重启不等于操作系统和磁盘全冷。修复说明明确冷缓存、样本不完整或估计区间重叠时可回退原排序,且采样不额外发起存储读取。
    • 扩大至 150 万行、保持相同倾斜比例:三个独立数据集无提示计划均选 ix_flow,索引分别读 455,769、455,431、454,807 行,主表各读 1,500,000 行;排除 ix_flow 后均选 ix_task,索引和主表各读 1 行,结果一致、id=1000000。第一个数据集随后连续 10 次仍选 ix_flow;交替测量 10 次,本机自动路径与对照中位数分别约 15.26 ms、0.46 ms。时间仅描述本机和当时负载,不是生产 SLA;未证明每次回退的具体采样状态。
    • 相关 objectio、planner 和 disttae 的 5 个定向测试各运行 3 次通过,涵盖元数据缺失、部分缓存、取消、重叠区间与计数;缓存测试断言采样不产生额外磁盘读取。15 万行正反倾斜 MOTR 回归在 4.2-dev 本地 runner 3/3 通过,已追加 MOTR #374 并接入 4.2 每日 job;合入后才定时执行,当前新提交 CI 仍在运行。150 万行证据保存在本地 qa-42-20261124-runtime/issue33088-1500k-{1,2,3}.json。

    待解决点

    确认 150 万行持续回退时的实际缓存命中/采样覆盖和估计区间,并明确在该规模与缓存条件下的产品性能验收标准;继续跟进 #374 的 4.2 专项 CI 和独立环境大数据测试。当前数据不足以区分设计取舍与需要研发修复的问题。

  4. XuPeng-SH commented on Nov 24, 2026

    @XuPeng-SH
    Contributor

    补充已有 QA 交接,不改派:#33093 已合入 main(ef65405e),新增 main 相邻倾斜样本与繁忙缓存回退、runtime-filter 容量边界。作者报告在相邻样本中索引从 49,152 行、主表从 8,192 行降至各 1 行,六列、排序及 LIMIT 不变;旧 head 2cb327a2 的独立复审核了容量边界,最终 d9631e74 有连接器 E2E 但未确认全套 Required,本轮未重跑。

    请分支分别记验收。main 复验两套倾斜样本、密集键与反向倾斜;并验证繁忙/冷/部分缓存回退不引入存储读取,以及预计键数跨现有 runtime-filter 容量的边界。此前 4.2-dev 的 150 万行持续回退仍独立保留,不能用 main 合并或通过结果替代。

  5. Ariznawlll commented on Nov 24, 2026

    @Ariznawlll
    Collaborator

    结论

    按原 Issue 的 15 万行、两个已有非唯一索引在数据倾斜下的自动选路问题,main 与 4.2-dev 均验收通过,关闭本 Issue。main 原始场景三轮均自动选择 ix_task,索引与主表各读 1 行;4.2-dev 专项 MOTR 也已在 Linux CI 成功执行且没有 skip。

    此前评论中的 150 万行扩展场景仍未通过,也不能写成全规模验收通过:代码核查确认当前点查采样仅允许 8,192 < TableCnt <= 262,144,150 万行会直接按保守设计回退原索引排序,而不是进入缓存采样。这个规模上限需要作为独立性能支持范围处理,不再阻塞原 15 万行缺陷的关闭。

    main 独立复验

    测试版本:MatrixOne 官方 main@568f0f4fdee3c34373f27b04316a2adf7d32a78a,已确认包含 #33093 的 merge commit ef65405e6f96d639da6da3c7e1c6b64bfa4c6070。mo-service -version 与运行中 git_version() 一致;使用独立单 CN / TN / LogService、端口和数据目录。

    • 原 Issue 的建表、15 万行插数、ANALYZE TABLE 及查询保持不变;首次竞争索引查询前仅执行三个键列的 sum 预热,避免把允许的冷缓存回退误判为修复无效。
    • 三个独立数据集 3/3:无提示 EXPLAIN ANALYZE 均选择 ix_task,索引 scan inputRows/outputRows=1/1,主表 scan 也为 1/1。
    • 自动路径与 IGNORE INDEX (ix_flow) 对照的六列结果完全一致:id=100000,三个等值谓词、ORDER BY created_at,id 和 LIMIT 1 均保留。
    • 密集键 task_id=7 三轮均与对照结果一致,id=26590。
    • 研发 5 万行 regular_index_point_skew.sql 连续三轮 57/57 statements 通过,覆盖稀疏键选 ix_task、密集键结果/排序,以及反向倾斜保留 ix_flow。

    缓存与容量边界

    #33093 新增的 5 个定向测试在 main 上以 go test -race -count=3 执行,合计 15/15 通过:

    • 繁忙内存缓存 shard 拒绝采样;
    • 缓存列元数据不完整时回退并正确释放;
    • 估计区间不重叠才允许替换;
    • 预计键数超过 runtime-filter/backfill 容量时保留原计划;
    • 多候选索引在同一批主表行上计算联合匹配数。

    这些测试与研发 BVT共同覆盖交接要求的繁忙/部分缓存回退、容量保护及正反倾斜;采样仍只读已有缓存,不为规划额外发起存储读取。

    4.2-dev 与回归去向

    • 此前 4.2-dev 原始 15 万行场景已经复测通过,稀疏键自动选 ix_task,索引和主表各读 1 行,结果与对照一致。
    • 最新 Linux 4.2 daily #45411167762 使用官方 4.2-dev@9ed1645371c63ad2c417022de9e4d56db8697ecc,运行版本已与 checkout 核对;issue_33088_42_index_skew 强制要求 success=1 failed=0 skipped=0,整 job 成功。
    • 4.2 回归保存在 MOTR script/14_issue_regression/issue_33088_42_index_skew.py,由 qa-32928-42-daily.yml 执行;main 研发 BVT 位于 test/distributed/cases/optimizer/regular_index_point_skew.sql/.result。

    未覆盖边界

    150 万行不是本轮通过项。此前三个 4.2-dev 数据集仍选择 ix_flow 并扫描 150 万行主表;现在已能解释其直接原因是超过 262,144 行硬上限,而不是“缓存尚未预热”。若产品要求在该规模及更大表上同样自动选择窄索引,需要另行评估扩大采样范围、成本与冷缓存策略,不能以本关闭评论代替该扩展验收。

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

Metadata

Metadata

Assignees

Labels

kind/performanceseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions