Skip to content

[Performance]: LIKE 'prefix%' never uses an index #31447

Closed

Description

@Ariznawlll

Description

In MySQL, col LIKE 'abc%' is optimized into a range scan on any B-tree index over col (prefix match → [abc, abd)). In MatrixOne, the same predicate never uses a secondary index — even when the indexed column is exactly the LIKE's subject and the pattern is a literal prefix:

CREATE TABLE full_idx (id INT PRIMARY KEY, name VARCHAR(100), KEY k_full(name));
EXPLAIN SELECT * FROM full_idx WHERE name LIKE 'aaaaaaaaaa%';

MO plan:

Project
  ->  Table Scan on fidx.full_idx
        Filter Cond: prefix_eq(full_idx.name)
        Block Filter Cond: prefix_eq(full_idx.name)

The optimizer clearly recognizes the predicate as a prefix condition — the internal prefix_eq node appears in the filter — but the plan is a bare table scan with no Index Table Scan.

Controls proving this is a missing rewrite, not a general index-usage fault:

predicate plan
name LIKE 'aaaaaaaaaa%' Table Scan only (prefix_eq in filter)
name >= 'aaaaaaaaaa' AND name < 'aaaaaaaaba' (manual equivalent range) Index Table Scan on k_full with prefix_in_range + serial_extract bounds ✓
name = 'aaaaaaaaaaXXXXXXXX' Index Table Scan on k_full ✓
LEFT(name,10)='aaaaaaaaaa' Table Scan (functional-expr path not rewritten either; separate surface)

So the index, the range-lowering machinery, and equality binding all work; only the LIKE 'prefix%' → range rewrite is absent. Same behavior with a prefix-limited index (KEY (name(10))).

Reproduction

CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100), KEY k(name));
INSERT INTO t VALUES (1,'aaaaaaaaaaXXXXXXXX'),(2,'bbbbbbbbbbYYYYYYYY');
EXPLAIN SELECT * FROM t WHERE name LIKE 'aaaaaaaaaa%';   -- Table Scan, no index
EXPLAIN SELECT * FROM t WHERE name >= 'aaaaaaaaaa' AND name < 'aaaaaaaaba'; -- Index scan

Impact

  • Any application using LIKE 'prefix%' for autocomplete / name search / pagination on an indexed column silently degrades from index range scan to full table scan. On large tables this is a performance cliff that differs from MySQL.
  • The presence of prefix_eq in the plan shows partial intent existed; the rewrite to index range was just never wired.

Expected behavior

A literal-anchored LIKE 'prefix%' over an indexable column should compile to the same index range scan the manual >= / < range produces (matching MySQL's range access type).

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。当前 main 的前缀 LIKE 仍走表扫描,而等价范围条件可使用同一索引;本轮结果正确,问题是访问路径及额外扫描。

    关键证据

    • main@d39d8a40343ccd40bf665036388df7a40ee867f8,隔离双 CN / 单 TN;按本 issue 的原始两行 SQL 在 3 个新库复测,3/3 得到同样的表扫描与索引扫描计划,结果均为第一行。
    • 扩展为三个各 30,000 行的新数据集,包含 NULL,LIKE 'abc%' 与 name >= 'abc' AND name < 'abd' 各交替执行 6 次:MO 与 MySQL 8.3 的计数和 ID 求和均一致。MO 前者走表扫描、后者走索引;MySQL 两者均使用 range 索引。MO 本机暖缓存中位数分别约 4.94–5.16 ms 和 0.98–3.01 ms,不能作为生产 SLA。
    • 可直接复用本 issue 上方的原始建表、数据及两条 EXPLAIN SQL 复现;错误列名被拒绝,数据摘要和连接保持正常。

    待解决点

    • 优化前缀 LIKE 索引访问,并继续验证字符集、转义、不同选择率和更大规模;本次未证明所有 LIKE 模式都可等价改写为范围。
  5. XuPeng-SH commented on Nov 2, 2026

    @XuPeng-SH
    Contributor

    duplicate with #31867

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions