Skip to content

[Performance]: VALUES join ignores FORCE INDEX and scans the base table #32460

Closed

Description

@XuPeng-SH

Summary

A batched lookup joined to a small VALUES relation scans the event base table even with FORCE INDEX and equality on the leading composite-index columns. The equivalent individual lookup uses the composite index. This is an execution-plan/performance issue; no incorrect query result has been observed.

Environment

  • matrixorigin/matrixone:4.2.8, disposable local test instance, isolated schema.
  • VERSION(): 8.0.30-MatrixOne-v4.2.8; GIT_VERSION(): 10d204b.
  • Reproduced on an empty table, so the output below demonstrates the plan, not production latency or measured scan rows.

Reproduction

CREATE TABLE event_index_repro (
  id BIGINT PRIMARY KEY,
  user_id VARCHAR(128) NOT NULL,
  run_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(32) NOT NULL,
  event_idx BIGINT NOT NULL,
  INDEX idx_control (user_id, run_id, event_type, event_idx)
);

EXPLAIN
SELECT r.ordinal, MAX(e.event_idx)
FROM (VALUES ROW(0, 'owner_a', 'run_a', 1, 10),
             ROW(1, 'owner_b', 'run_b', 2, 10)) r(ordinal, uid, rid, lo, hi)
JOIN event_index_repro e FORCE INDEX (idx_control)
  ON e.user_id = r.uid AND e.run_id = r.rid
 AND e.event_type = 'user_intent'
 AND e.event_idx > r.lo AND e.event_idx <= r.hi
WHERE e.run_id IN ('run_a', 'run_b')
GROUP BY r.ordinal;

Actual plan contains Table Scan on ...event_index_repro with run_id IN (...) and event_type = 'user_intent' filters, and no index scan. On long histories, this risks work proportional to the base table even when only a few tenant/run keys are requested.

Control query:

EXPLAIN
SELECT MAX(event_idx) FROM event_index_repro FORCE INDEX (idx_control)
WHERE user_id = 'owner_a' AND run_id = 'run_a'
  AND event_type = 'user_intent' AND event_idx > 1 AND event_idx <= 10;

The control uses Index Table Scan on event_index_repro.idx_control with the equality prefix and range filters.

Expected

The optimizer should consider bounded composite-index lookups for each VALUES key, especially with FORCE INDEX, rather than scanning the base event table. Please clarify if the hint cannot apply to this join form.

Workaround

A single query with one parameterized, index-filtered SELECT MAX(...) per key joined by UNION ALL uses an index scan for each branch in the tested version. This avoids the observed base table scan but increases the size and per-key work of the query; it is not a demonstrated production performance benchmark.

Activity

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

    @gouhongshen
    Contributor

    PR #32490 is trying to fix this issue.

  4. gouhongshen commented on Nov 13, 2026

    @gouhongshen
    Contributor

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

  5. gouhongshen commented on Nov 17, 2026

    @gouhongshen
    Contributor

    PR #32832 is trying to fix this issue.

  6. gouhongshen commented on Nov 17, 2026

    @gouhongshen
    Contributor

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

  7. XuPeng-SH commented on Nov 17, 2026

    @XuPeng-SH
    ContributorAuthor

    QA 交接(不是验收通过)

    修复 #32832 已于 2026-11-17 03:17 UTC 合入 main,merge SHA 47957ef450850e3258058da3fca0ee66edd96244。本次修复允许可精确物化 runtime filter 的连续复合索引前缀用于 join,覆盖原 VALUES 查询及强制索引表位于 build 侧的情况;仅在可构造有界访问时调整方向,保留非 leading 等值场景的 generic FORCE INDEX fallback、基数选择方向、完整键优先及 residual。已读完整 issue/PR 讨论与最终评审,未见适用于当前修复的已验证反例或未完成开发 follow-up。本 issue 非 CI flaky,保持 open 交 QA,不自动 backport。

    验证证据与限制:本轮仅远端核对,没有本地实测。作者报告原始基线、build-side 修复前与方向门控前的回归测试分别失败,修复后 11 项 focused planner 测试和完整 pkg/sql/plan 通过,vet/lint 通过。前一服务 revision 的两轮 mo-tester 均为 14/14,并检查了 MatchPrefix、双分量 serial_full、填充结果及 UNION ALL/IGNORE INDEX 对照;最终仅改方向的 planner revision 未重跑服务 BVT 或完整 mo-service build。最终评审认可修复并引用冻结 head 的 UT/SCA/shared-build/multi-CN BVT 成功汇总,但未读取各 CI 日志;评审本地 planner 测试因依赖下载超时未实际运行,SQLite fixture oracle 不等于 MatrixOne 执行。以上均不冒称合并 SHA 的本轮实测。

    建议 QA 在上述 main SHA 或后继版本复核原 VALUES 查询的有界 idx_control 访问与 runtime-filter 配对;覆盖两侧 join 方向、非 leading fallback、大表 probe/小表 build、完整键优先、partial unique 拒绝、NULL/重复/未匹配键、残余范围条件及标量/UNION ALL/IGNORE INDEX 结果等价。原报告为空表计划问题,现有计划与小规模语义证据不代表长历史数据的生产延迟、吞吐或扫描行数改善;这些性能量化仍待 QA,不阻止本次交接。

  8. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    Verified fixed on main tip 919c9fc32f.

    Re-ran the exact repro from this issue on a self-built main@919c9fc32f instance (isolated ports, fresh schema). The plan for the batched VALUES join under FORCE INDEX (idx_control) now contains:

    Join Type: INDEX
      ...
      ->  Table Scan on event_index_repro          [Runtime Filter Probe: e.id]
      ->  Index Table Scan on event_index_repro.idx_control [Runtime Filter Probe: __mo_index_idx_col Match Prefix]
    

    i.e. the composite index is used for the join lookup instead of scanning the base table with only the run_id IN (...) / event_type filters. This matches fix #32832 ("preserve composite index access for joins", merged as 47957ef); the official regression file test/distributed/cases/optimizer/issue_32460.sql also runs clean (0 errors) on main tip.

    Note the reported issue targeted 4.2.8 (10d204b) — a 4.2-dev backport of #32832 would be needed to close this for the 4.2 line as well; on 4.2-dev@e8fd84b331 this plan-level check has not been re-verified yet.

  9. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    Closing as verified fixed on main tip 919c9fc32f (see verification comment: VALUES-join plan now uses Join Type: INDEX + Index Table Scan on idx_control per fix #32832; official regression issue_32460.sql passes clean). 4.2 line remains unfixed pending backport — if needed, track separately for the 4.2 release.

  10. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    Regression test added to matrixorigin/motr (follow-up to the closure).

    A case pinning this fix's semantics is now in motr (suite 14_issue_regression, target-branch: main): issue_32460_values_join_force_index —

    • the batched VALUES join under FORCE INDEX (idx_control) must return exactly the index-congruent rows in range,
    • the identical query without the hint must return the same results (semantic equivalence of the index path),
    • the single-key control lookup keeps returning its boundary rows,
    • EXPLAIN with the hint builds without error (plan text intentionally not asserted, per motr conventions).

    Recorded baseline and 2x repeat verification both pass on main@919c9fc32f (stmts 20/20 ok). The plan-level assertion (Join Type: INDEX / Index Table Scan on idx_control) remains covered by test/distributed/cases/optimizer/issue_32460.sql in the matrixone repo itself.

  11. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    Regression case landed in motr via matrixorigin/motr#355 (target-branch: main).

  12. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    重新打开:本 issue 存在“先关闭、后补 motr PR”的顺序问题。对应的 motr #355 于 2026-11-17 10:09 UTC 创建,晚于本 issue 的关闭时间,不符合“关闭前必须完成回归 PR”的要求。现恢复 Open。当前 #355 已包含本 issue 的用例,后续需核对该 PR 的完整验证与交付状态后,再统一给出关闭结论。

  13. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    QA 验收结论:main 与最新 4.2-dev 的原问题及扩展检查通过

    实际验证版本与修复来源

    • main:7fc45a2be2424d887199c5949362855cbacfe30f,SQL 确认 git_version() 与该 SHA 一致,version() = 8.0.30-MatrixOne-v1.3.0。包含 #32832,merge commit 47957ef450850e3258058da3fca0ee66edd96244。
    • 4.2-dev:7400cae392e90fa94b8cf54dc8520c3c13647884,SQL 确认 git_version() 与该 SHA 一致,version() = 8.0.30-MatrixOne-v4.2.9。包含 #32490,merge commit e64edc55347b8e87823cbcb7b5d3938ebaec2247。
    • 两个分支均使用隔离的单 LOG/TN/CN 实例,独立端口和数据目录。

    原始问题与结果语义:通过

    原始 VALUES 与 FORCE INDEX (idx_control) 的 join,以及 reverse SQL-side join,在两个分支均同时出现:

    Join Type: INDEX
    Index Table Scan on event_index_repro.idx_control
    Runtime Filter Probe: __mo_index_idx_col Match Prefix
    Runtime Filter Build: serial_full(...)
    Runtime Filter Probe: e.id
    Table Scan on ...event_index_repro
    

    这里最后的基表扫描是 INDEX JOIN 所需的主键 backfill,与原问题中只有基表过滤扫描、没有复合索引 lookup 不同。VALUES 与 scalar per-key UNION ALL workaround 的结果一致;NULL、重复/未匹配键、residual、非 leading fallback、完整键优先和 IGNORE INDEX 对照均符合预期。

    4.2-dev 的 22 项检查最终 22/22 通过。 首次 21/22 的标量对照失败已定位为脚本对 main 计划文本的依赖:4.2 实际计划使用 Index Table Scan on idx_control、prefix_eq 以及 serial_extract(...,3,BIGINT) > 1 / <= 10 的有界范围条件,而脚本只接受 prefix_in_range。没有把它当作产品失败,也没有删除该检查;已兼容两种有界计划表达后在 4.2-dev 和 main 分别重跑完整 22 项,均通过。

    白盒与 motr 证据

    未验证:多 CN、race、生产长历史数据的延迟/吞吐/扫描行数量化和长稳。当前结论覆盖原问题的计划与结果语义,不夸大为生产性能基准;物理 build 侧方向由 planner 白盒覆盖,reverse SQL 文本顺序不等同于所有物理 join 方向的黑盒验证。

  14. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    按上述 QA 验收结论关闭:main@7fc45a2 与 4.2-dev@7400cae392 的原始计划及 22 项扩展检查均通过;9 个 planner 白盒测试通过;motr #355 的 record/replay/repeat 通过且新 head 4 项 CI 全 SUCCESS。未覆盖的生产性能量化、多 CN、race、长稳边界已在验收 comment 中明确。

  15. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    补充 main 物理计划断言增强

    在已完成 main/4.2-dev 双分支验收及 motr#355 交付的基础上,main 专用 motr#358 新增 issue_32460_force_index_plan.py 及有效 baseline。

    该增强用例除结果等价外,要求实际计划包含 idx_control、join type: index 和 index table scan,避免仅凭 SQL 可执行或结果一致就认定 FORCE INDEX 生效。固定 main 919c9fc6e3dc15e2d2e5c905078bd05dff24e2bc 两轮复跑通过,干净交付工作树再次通过,无 SKIP/FAIL;#358 的 4 项离线 CI 全部通过。

    #358 当前 OPEN、尚未合入;这是对既有验收的回归增强,不改变此前双分支验证结论,也不把较早 #355 的创建时间描述成早于原关闭。

  16. Ariznawlll commented on Nov 18, 2026

    @Ariznawlll
    Collaborator

    Supplemental MOTR coverage: 4.2-dev now schedules both regressions

    This is a regression-coverage supplement, not a new product acceptance or a request to reopen this completed issue.

    • MOTR PR: https://github.com/matrixorigin/motr/pull/358
    • Supplemental commit: 35f7093a1768cc14e752aedad855bca875311763
    • Both 14_issue_regression.issue_32460_force_index_plan and 14_issue_regression.issue_32460_values_join_force_index are now discoverable under --branch 4.2-dev, with dedicated .4.2-dev.out baselines. They previously had @target-branch: main, so manual execution against a 4.2 service did not create a scheduled release-branch regression.
    • Actual 4.2-dev runtime: git_version()=7400cae392e90fa94b8cf54dc8520c3c13647884, version()=8.0.30-MatrixOne-v4.2.9.
    • Release-golden record run-1794977163758207000: 2/2 cases passed; replay run-1794977164388095000: 2/2 passed; repeat-two replay run-1794977165019394000: 4/4 passed, SKIP=0. Each run performs 22 detailed plan/semantic checks plus 2 focused checks; the script-level report's stmts=1 is the scenario wrapper count, not the number of internal checks.
    • Main control remains green on git_version()=7fc45a2be2424d887199c5949362855cbacfe30f: related-case record run-1794977083538308000 5/5, 53/53 statements; replay plus two repeats run-1794977121727834000 10/10, 106/106 statements, SKIP=0.
    • WITH RECURSIVE CTE referencing recursive table twice crashes server (fatal error: stack overflow) #32795 remains main-only in MOTR. Its original SQL still crashes 4.2-dev and is not silently skipped or presented as a passed release regression.

    CI for the exact supplemental head completed successfully: https://github.com/matrixorigin/motr/actions/runs/44240301802 — Python 3.10/3.12/3.13 offline contracts and the offline Go protocol/numeric contracts, 4/4 SUCCESS. A full local offline sweep is not claimed: the host forbids ps for the pre-existing ScriptLifetimeTest; the two new branch-selection tests pass and the complete Linux CI gate passes. No full TKE rerun or new production-scale performance claim is made.

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions