Skip to content

[Performance]: Index range bounds degrade to opaque block filters #32209

Closed

Description

@Ariznawlll

Environment

  • MatrixOne main@HEAD (c6683bd), 3-CN local cluster
  • big8m (k INT PRIMARY KEY, k2 INT, KEY idx_k2(k2)) — 30,000,000 rows; big1m similarly indexed (1M rows)

Reproduction

SELECT COUNT(*)
FROM big8m t1 JOIN big1m t2 ON t1.k2 = t2.k2
WHERE t1.k2 < 100;

EXPLAIN ANALYZE on the default plan:

-> Join (INNER)  Join Cond: serial_extract(...) = t1.k2
     inputRows=2,101,000  outputRows=2,001,000,000  waitTime=819ms
   -> Table Scan on big8m
        inputBlocks=2452  inputRows=20,086,784  outputRows=2,001,000
        Filter Cond: (t1.k2 < 100)
        Block Filter Cond: (t1.k2 < 100)     ← pruned only ~1/3; 20M of 30M rows read
   -> Index Table Scan on big8m.idx_k2     ← used for join-back
        inputRows=1,000,424   ← THE WHOLE INDEX TABLE
        Block Filter Cond: (__mo_index_idx_col < '<opaque>')
   -> Index Table Scan on big1m.idx_k2
        inputRows=100,000  Filter: serial_extract(...) < 100  (row-level residual)

Three compounding defects:

  1. Range bounds degrade to <opaque> block filters. The serialized range bound for k2 < 100 fails to fold into a usable block-filter condition — the display shows '<opaque>' and block pruning on the base scan barely works (20M of 30M rows read for a predicate selecting 2M). This is the default (non-forced) plan.
  2. Index-table scans read the entire index and filter by per-row serial_extract residual — the equality path generates healthy prefix_eq filters, the range path does not fold into prefix_in_range here (asymmetric with applyIndexJoin's replaceRangePairCondition, which does fold ranges — see also the FORCE INDEX variant of this bug, [Performance]: FORCE INDEX range predicate reads whole index table #32129).
  3. The join then evaluates serial_extract(...) per row to compare keys — 2.1M inputs producing 2 billion outputs at 819-1447 ms of wait.

Impact

Time-window / bucketed-key joins with any range predicate are the standard fact-join shape. With correct bound serialization + prefix range folding + block pruning, this query reads a few thousand blocks instead of ~4,700; 0.5 s+ plan instead of multi-second wall time dominated by ~2 GB of decode work.

Suggested fix direction

  • Fold first-column range predicates on secondary indexes into prefix_in_range block filters on the serialized key (same mechanism replaceRangePairCondition already emits).
  • Fix serialized-bound block filters that currently emit '<opaque>' and prune nothing.
  • Consider comparing serial keys directly in the join instead of serial_extract per row per side.

Activity

  1. added this to the MO-202611 milestone on Nov 5, 2026
  2. added
    kind/bug
    on Nov 5, 2026
  3. added theissue type on Nov 5, 2026
  4. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    Contributor

    Consolidating #32129 here: both expose missing prefix_in_range conversion for secondary-index first-column range predicates. Keep #32129’s FORCE INDEX(idx_g) case (30M entries; full index-table scan and per-row serial_extract) as a distinct acceptance case alongside #32209’s default range/join path. Verify both paths produce safe, selective range bounds. #32063 concerns a range on the second column of a composite index and remains separate.

  5. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    Contributor

    Consolidating #32063 here as the composite-index range-bound case of the same serialized-key pruning work. Preserve separate acceptance cases for (1) a range on the first indexed column, including the FORCE INDEX path, and (2) equality on a leading column followed by a range on the next column. The compound-range case must retain exact result semantics and avoid scanning the whole leading-key prefix.

  6. added
    severity/s0
    ai-easy
    and removed on Nov 10, 2026
  7. gouhongshen commented on Nov 10, 2026

    @gouhongshen
    Contributor

    PR #32503 is trying to fix this issue.

  8. gouhongshen commented on Nov 11, 2026

    @gouhongshen
    Contributor

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

  9. XuPeng-SH commented on Nov 11, 2026

    @XuPeng-SH
    Contributor

    转 QA 验证。PR #32503 已合入 main,merge SHA 5a5422598a7966c570f84e4f2923fc9361b359c3。请保留本项整合的 #32129 FORCE INDEX 首列范围及 #32063 等值前缀 + 后续列范围作为独立验收用例。

    修复提供序列化范围边界及精确行侧残余检查,保留 NULL 唯一索引完整性、字符串/二进制边界及错误求值顺序保护。旧 head 1778721a 的 throwing suffix 反例已在 9103f5188a 修复,后续评审报告对应 base/candidate SQL 和 planner 控制通过。已重读完整 issue 评论及 PR 评审,未见适用于合并修复的已验证反例或未完成开发 follow-up。

    已有证据(作者/评审报告,非本轮实测):作者报告完整 planner 包通过,BVT 两轮均 111/111;评审报告 11 个 planner 测试和 memory/persisted SQL 控制通过,同一复合范围扫描从 base 4 blocks/7 rows 减至候选 2 blocks/4 rows且结果一致。最终 head 的 Ubuntu UT 曾在 TestViewDescriptionSubscription 失败,已单独记录 #32424;不能把该记录当作本轮全 CI 通过。

    待 QA:在合并版本覆盖默认 range/join、FORCE INDEX 有界 COUNT(DISTINCT)、复合等值前缀后范围,并比较未索引基准的结果和扫描工作量;覆盖空/闭区间、NULL、重复值、nullable unique、VARCHAR/VARBINARY 含 NUL 边界、throwing bound 短路、回滚/重连和多 CN。原 30M 行三 CN 延迟基准未重跑,不承诺普遍耗时改善。<opaque> 是保留的字面量隐藏,默认 join 仍使用 serial_extract,二者不应单凭显示形态判定范围修复失败。本轮未运行本地测试;转测不代表验收通过。

  10. Ariznawlll commented on Nov 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    原问题验证

    对 k2<3(10% 选择性)的二级索引范围:

    1. EXPLAIN ANALYZE 实测:Index Table Scan on big8m.idx_k2 实际只读 1 block / 3000 行(全表 4 blocks / 30000 行),k2<30 全范围对照读满 30000 行——序列化范围边界已正确折叠进 block filter 并精确裁剪。
    2. issue 的 join 形态 big8m t1 JOIN big1m t2 ON t1.k2=t2.k2 WHERE t1.k2<3:两侧索引扫描各只读 3000 行(修复前读整张索引表),结果 3000000 与无索引控制表完全一致。
    3. 说明:EXPLAIN 文本中 __mo_index_idx_col < '<opaque>' 是序列化字节串的显示渲染,不代表边界失效;实际裁剪以上述 inputRows/inputBlocks 实测为准。

    Happy Path 覆盖

    1. 默认计划、FORCE INDEX 计划([Performance]: FORCE INDEX range predicate reads whole index table #32129)均走有界 Index Table Scan,非全索引扫描。
    2. 复合索引 KEY(a,b) 上 a=9 AND b>90:Block Filter 折叠为 prefix_in_range(__mo_index_idx_col)([Performance]: composite index second-column range prunes nothing, constant prefix scan #32063 核心修复点),>=、<、<=、BETWEEN、开区间 5 个变体结果与无索引控制表逐行一致。
    3. COUNT(DISTINCT k2) ... FORCE INDEX 走有界索引扫描,不再退化到全 covering index 扫描(修复的 hinted covering fallback 问题)。

    Unhappy Path 覆盖

    1. 空等值前缀 a=99 AND b>90 返回空集且不报 20203(filter-stability 护栏保留)。
    2. 复合索引第二列 NULL 的行被范围正确排除(与 MySQL 语义一致)。
    3. 首列含 NULL 的行不进入复合范围结果。

    白盒验证

    核对 #32503 修改范围:pkg/sql/plan/apply_indices.go 序列化范围游标边界 + 精确行侧残余检查,保留 NULL 唯一索引完整性、字符串/二进制边界及错误求值顺序保护。上游 apply_indices_test.go 新增 416 行;composite_index_range BVT(165 行 SQL)覆盖同族场景。

    Go 单测

    • TestRegularIndexCompositeSuffixRangeBounds、TestRegularIndexUniqueSuffixRangeRequiresCompleteRowSource(pkg/sql/plan)ok;焦点 -race 通过。

    回归测试

    已追加 matrixorigin/motr#349:14_issue_regression/issue_32209_index_range_bounds(Python 场景,18 项断言),覆盖首列范围裁剪实测、join 两侧有界、复合 prefix_in_range、5 个范围变体对照、空前缀护栏、NULL 排除、COUNT(DISTINCT) 有界。录制后普通 diff 模式 3 并发连续多轮通过;PR 4 项 CI 全部成功。用例标记 @target-branch: main(修复未 backport 4.2-dev)。

    验证边界

    本地为 30k/10k 行缩比数据(模式等价,block 裁剪可观测);未重复 issue 的 30M 行 / 3-CN 规模与 EXPLAIN ANALYZE waitTime 对比。多 CN 下序列化边界一致性未在本次范围。

    测试结论

    测试通过,关闭本 issue。

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions