Skip to content

[Bug]: Prefix indexes under-fetch FORCE INDEX BETWEEN ranges #26841

Closed

Description

@LeftHandCold

Reproduction

CREATE DATABASE prefix_range_repro;
USE prefix_range_repro;

CREATE TABLE t (
  id INT PRIMARY KEY,
  s VARCHAR(32),
  KEY idx_s(s(3))
);
INSERT INTO t VALUES
  (1, 'abcX'),
  (2, 'abcY'),
  (3, 'abdA'),
  (4, 'abbZ');

SELECT GROUP_CONCAT(id ORDER BY id)
FROM t FORCE INDEX(idx_s)
WHERE s BETWEEN 'abcX' AND 'abdA';
-- actual:   3

SELECT GROUP_CONCAT(id ORDER BY id)
FROM t IGNORE INDEX(idx_s)
WHERE s BETWEEN 'abcX' AND 'abdA';
-- expected: 1,2,3

The same result was reproduced on current main (45a917d7f8) and on PR #26829 (b1cd7bb3e1), so this is pre-existing and is not caused by that PR.

Expected behavior

A declared prefix index is a lossy candidate structure. FORCE INDEX may change the access path, but it must not change the SQL result. Every row whose full value is in the requested range must remain a candidate and be rechecked against the base-table predicate.

Actual behavior and likely boundary

The hidden table stores substring(s, 1, 3). The range rewrite compares that truncated key against the untruncated BETWEEN bounds and then does not retain the leading BETWEEN predicate as a base-table residual. Values abcX and abcY are excluded before the base table can recheck them.

A safe fix is to avoid range/BETWEEN index rewrites for declared prefix parts, or derive a conservative candidate condition and retain the original base-table range predicate.

Scope / duplicate check

Activity

  1. self-assigned this
    on Aug 9, 2026
  2. LeftHandCold commented on Aug 11, 2026

    @LeftHandCold
    CollaboratorAuthor

    Post-merge validation result.

    Although this issue is already closed, I independently verified its fix on the exact #26829 merge commit ac339444b558daf2c3d7fe63f755743449db7696.

    The original 08770e1 baseline under-fetched through FORCE INDEX (BETWEEN returned only id 3 while IGNORE returned ids 1,2,3). On the merge commit, FORCE equals IGNORE for BETWEEN, IN, single/paired ranges, strict bounds, OR, prepared runtime bounds, unique prefix indexes, scoped ORDER/GROUP hints, rename plus DML, and post-flush persisted blocks. EXPLAIN confirms lossy declared-prefix range paths fall back to a base-table scan, while a complete-index control remains eligible.

    The later #26852 PR adds regression coverage; the production correctness fix is present in #26829.

    Conclusion: confirmed fixed; keeping the existing closed state.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions