Skip to content

[Performance]: index join-back never yields to full scan, 200x slower at 10% selectivity #32015

Description

@Ariznawlll

Summary

The index join-back (Index Table Scan + row-by-row join back to the base table) costs ~4-14 µs per row — roughly 6000× the per-row cost of a vectorized full scan — and the planner chooses it at every selectivity, never falling back to a full scan. At 10% selectivity the index path is 200× slower than scanning the whole table.

Environment: main@HEAD (c6683bd), etc/launch-multi-cn. Table big1m: 1,000,000 rows, KEY idx_k2 (k2); full-scan COUNT(*) baseline = 2.2 ms. Results identical in all rows below.

predicate (hits) chosen plan latency vs full scan 2.2 ms
k2 = 42 (1,000) idx_k2 join-back 6.7-13.8 ms 3-6× slower
k2 >= 42 AND k2 < 44 (2,000) idx_k2 join-back 12.9 ms 5.9× slower
k2 >= 42 AND k2 < 47 (5,000) idx_k2 join-back 21.5 ms 9.8× slower
k2 >= 42 AND k2 < 62 (20,000) idx_k2 join-back 77.4 ms 35× slower
k2 >= 42 AND k2 < 142 (100,000) idx_k2 join-back 439.7 ms 200× slower

Crossover math: join-back ≈ 4.4 µs/row linear, full scan of the entire 1M-row table = 2.2 ms — so the index path loses as soon as the result exceeds ~500 rows (0.05% selectivity), yet the plan shape never changes (Index Table Scan in EXPLAIN at every row count).

Why this compounds with #31994

Because selectivity estimates assume uniformity (#31994), the planner also misjudges the hit count for skewed values — so a hot value estimated at 100 rows (assumed cheap join-back) actually retrieves 90,000 rows through the 4.4 µs/row path: a 900× estimate error multiplied by a 6000× per-row cost error. We observed exactly this in the earlier skew experiment (294.8 ms index plan vs 234.8 ms full scan at only 100k rows).

Expected

The cost model needs a realistic join-back row cost (measured here at ~4-14 µs/row vs ~0.002 µs/row scan) and a selectivity threshold above which it emits a plain table scan + filter instead of index join-back. MySQL's analogous cutoff is typically around 5-20% selectivity; here even 0.05% already loses.

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    White-box root cause (main@HEAD):

    The planner does have a join-back cutoff — but it's calibrated for a cost model that is off by ~3 orders of magnitude. applyIndicesForFiltersRegularIndex (pkg/sql/plan/apply_indices.go:2444-2457) disables index backfill only when:

    ignoreStats := forceIndex || node.Stats.TableCnt < 50000
    if !ignoreStats {
        if node.Stats.Selectivity >= InFilterSelectivityLimit ||
           node.Stats.Outcnt >= float64(GetInFilterCardLimitOnPK(sid, node.Stats.TableCnt)) {
            allowBackfill = false
        }
    }

    with constants (pkg/sql/plan/runtime_filter.go:27-55):

    InFilterCardLimitNonPK   = 10000
    InFilterCardLimitPK      = 1000000
    InFilterSelectivityLimit = 0.3          // 30% selectivity
    GetInFilterCardLimitOnPK = clamp(tableCnt*0.3, 10000, 1000000)

    For the 1M-row table in the issue: selectivity 0.1 < 0.3 and outcnt 100,000 < 300,000, so allowBackfill = true and the index plan is built. The 0.3/30万 thresholds are reasonable for a B-tree world where a join-back row costs ~3-5× a scanned row. Measured on MO: join-back ≈ 4.4 µs/row vs vectorized scan ≈ 0.0022 µs/row — a 2000× per-row ratio, so the break-even is ~0.05% selectivity, not 30%. The thresholds are thus 600× too permissive: everything between 0.05% and 30% selectivity takes the index plan and loses to a full scan, increasingly badly (200× at 10%).

    Minimal lever: lower InFilterSelectivityLimit (and the PK card limit accordingly) to match the real join-back/scan cost ratio, or make the cutoff cost-based using measured per-row join-back cost instead of a flat constant. As a reference point, with the measured 2000× per-row ratio the correct cutoff is on the order of scan_cost_per_row / joinback_cost_per_row ≈ 0.0005 selectivity.

  5. added a commit that references this issue on Nov 3, 2026
    dd2499c
  6. XuPeng-SH commented on Nov 3, 2026

    @XuPeng-SH
    Contributor

    转 QA 验证:PR #32039 已合入 main,merge SHA dd2499cdcdf2722848851d4debca6cd6c66b4751。已核对本 issue 正文和全部 1 条原评论:原问题及白盒分析基于旧版本,PR 针对无显式 PK 回表的逐行逐 key 比较、workspace 候选编码/查找及自动回表成本准入作了修复;当前没有新版本已验证反例或未完成的本问题开发 follow-up。

    真实证据与限制:本轮仅远端核对,未做本地测试。PR 作者报告两 CN、1M 行暖缓存对照:无显式 PK 的 1000 命中 AUTO 约 201.4→1.14 ms,真实 PK 100000 命中 AUTO 47.2→2.26 ms,并保留聚簇有益索引路径;组件测试、八个所属包 normal/race、公开事务 SQL 回归及两次 CI merge build BVT(每次 201/201)通过。这些是作者/CI 报告,不能当成本轮实测或普遍性能保证。懒构建 map 仍有候选/reader 扇出内存成本,首次 O(K) 构建在事务锁下且不可中断。

    请 @Ariznawlll 在合并后版本以同一 SQL/结果对照 AUTO、FORCE INDEX、IGNORE INDEX,覆盖无 PK/真实 PK、选择率梯度、聚簇/散布布局、冷暖缓存、多 CN/DOP、宽 key、prepared 复用、workspace/落盘、重复行及 rollback;同时关注首次构建延迟、取消响应及内存。#31994 的偏斜统计问题是独立范围,本次不宣称已修复。合并后完整工作负载与线上表现尚未由本轮验证;本次为转测,不是验收通过。

  7. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。10% 命中场景在本次测试中已自动选择扫描并返回正确结果;但在无显式主键、0.1% 命中场景,AUTO 仍选非覆盖索引回表,在本机配对测试中持续慢于同条件全表扫描,尚未满足本 issue 对回表成本准入的验收。该结论限于下述数据和本地环境,不代表所有表或线上负载都有相同比例。

    关键证据

    • 当前版本:官方 main@dd2499cdcdf2722848851d4debca6cd6c66b4751,即 #32039 合入提交;macOS / Apple Silicon,独立 LOG/TN/CN 单进程,GOMAXPROCS=4、GOMEMLIMIT=4GiB。测试机器非 CPU 独占,不作为生产性能结论。

    • Happy case:分别构造无显式 PK 和真实 PK 的两张表,各 1,000,000 行,k2=id%1000、idx_k2(k2),查询非索引覆盖列 payload;校验 1,000、2,000、5,000、20,000、100,000 命中下 AUTO / FORCE / IGNORE 的完整 (id,payload) 行集与独立计算结果一致,覆盖重复键和落盘数据。两表 100,000 命中的 IN (42,...,141) 经 EXPLAIN 确认 AUTO/IGNORE 走扫描、FORCE 走索引回表;结果均为 100,000 行且 sum(length(payload))=1,388,842。

    • 10% 选择率(每模式 2 次预热 + 10 次交错计时,聚合结果只返回一行):无 PK AUTO / FORCE / IGNORE 中位数 3.41 / 5.49 / 3.46 ms,真实 PK 4.16 / 25.37 / 4.31 ms。强制方案的计划确实包含 Index Table Scan 和 INDEX Join;本轮结果支持该数据上广泛命中选择扫描,但不与不同机器和不同 SQL 的历史 200× 直接比较。

    • 待研发确认的 0.1% 对照:无显式 PK,select count(*),sum(length(payload)) ... where k2=42,AUTO 为 INDEX Join,IGNORE 为 Table Scan;同一连接交替执行,排除前 5 对预热后的 20 对中,AUTO 有 19/20 对较慢,AUTO / IGNORE 中位数 9.889 / 5.536 ms,配对耗时比中位数 1.811。查询结果均为 1,000 行对应的聚合值。这说明此次场景下仍有成本选择损失,不代表历史 200× 故障仍以原幅度存在。

    • Unhappy case:两张表各在事务中增加两条同索引键 workspace 行,比较非覆盖 AUTO / FORCE / IGNORE 行集;回滚后再次核对完整行集,结果恢复且无残留。

    • 对原始双边范围条件 k2>=42 AND k2<142,本环境的 FORCE、AUTO、IGNORE 三者 EXPLAIN 均为 Table Scan;因此不能把该 FORCE 耗时当成强制索引对照。上面的可验证强制索引对照使用等价 100 值 IN,并分别核对实际计划。

    • 白盒:最新 main 的 pkg/sql/plan 回表成本测试及 pkg/vm/engine/readutil 无序键、workspace 和并发首次构建测试各重复 3 次通过;这不消除上述实际计划的性能差异。

    待解决点

    • 请研发结合成本模型和目标工作负载复核无 PK / 0.1% 命中下的 AUTO 回表选择,在安静且可控的环境重复配对测试,并确认是否需要调整准入或说明该选择为预期。当前组件微基准不证明端到端生产回退。
    • 冷缓存、两 CN/DOP 梯度、宽键、prepared 复用、首次构建取消与峰值内存等完整场景尚未完成;不能凭本轮测试关闭该 issue。[Performance]: selectivity assumes uniform distribution: 900x off on hot values #31994 的统计偏斜仍是独立范围。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions