Skip to content

[Performance]: Covering index scans are never chosen #32127

Open

Description

@Ariznawlll

Environment

  • MatrixOne main@HEAD (c6683bd), 3-CN local cluster
  • Table: cov8m (k INT PRIMARY KEY, g INT NOT NULL, v INT NOT NULL, KEY idx_gv(g, v), KEY idx_g(g)) — 30,000,000 rows

Reproduction

SELECT k, g, v FROM cov8m WHERE g = 997 AND v < 50;

This query needs exactly (g, v, k) — all of which live in the idx_gv index table (non-unique secondary indexes are physically stored as (serialized_key, pk)). Nothing should touch the base table.

Measured

plan blocks read time
default 500 (whole base table) 805 ms / 191 ms
FORCE INDEX (idx_gv) — pure Index Table Scan 4 57-72 ms

10-14x on a small-result-set query; and it gets worse with table size because the default plan is O(table), not O(matches).

The default plan is always:

Project
  ->  Join: INDEX join (cov8m.k = __mo_index_pri_col), [ForceOneCN]
        ->  Table Scan on jumpstats.cov8m   (cost=4096000, 500 blocks — driver!)
        ->  Index Table Scan on cov8m.idx_gv

even when the SELECT list is just the PK (SELECT k FROM cov8m WHERE g=500 AND v<20000 — the PK is literally the index table's second column).

Root cause (code)

pkg/sql/plan/apply_indices.go:

  • :3973 indexCoversRequiredColumns computes coverage only from idxDef.Parts — each part column is marked in coveredCols; the physical primary key is never added to the coverage set.
  • :3429-3439 requiredCols = all columns referenced above the scan ∪ filter columns. For SELECT k ... WHERE g=... AND v<..., required = {k, g, v}. Since k (the PK) is never marked covered, matchRegularIndexOnlyScan (:4019) returns false and the index-only path is rejected → the planner falls back to the scan-driven INDEX join.
  • Contradiction inside the same file: resolveRegularIndexBackfillResidualAccess (:3240-3245) explicitly supports reading the physical PK from the index table (regularIndexResidualPhysicalPK). The executor knows the index table always carries the PK; the planner's coverage check does not.

DML impact (same defect)

DELETE FROM cov8m WHERE g = 500 AND v < 500 (deletes ~240 rows): base table full scan (500 blocks, ForceOneCN) driving the INDEX join, plus two more INNER joins to the secondary index tables for index maintenance — 549 ms for a tiny delete. DELETE cost is O(table), not O(matched rows).

Expected behavior

When the required columns are all in idxDef.Parts ∪ {PK}, choose an index-only scan (pure Index Table Scan) instead of the base-scan-driven join-back. FORCE INDEX proves the path exists and is 10-14x faster.

Related

#32015 (join-back family) — same family, different root: this is the missing covering-column detection (parts ∪ PK), not the join-back structure itself.

Activity

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

    @XuPeng-SH
    Contributor

    补充 #33501 合入版验证:在 main@0c5cf1775518e7974564eeb54b2157e5abc13aa6 上,5 组定向 planner 用例各 3 次通过(GOMAXPROCS=16),覆盖索引创建顺序、窄覆盖/宽键回表、点查/范围及 LIKE + IN 控制。真实 MySQL TCP 的 TestLikePrefixIndex 和大小写不敏感对照也通过(12.388 秒)。

    这证明本次 SELECT 计划选择的定向行为,不是工单原始 3,000 万行的性能 A/B;该 PR 没有解决 DELETE 的基础行 ID 路径,完整 CI 也未通过。保留当前研发负责人,不转测。

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