Skip to content

[Bug]: MySQL index hints return wrong rows or ignore missing composite indexes #25356

Closed

Description

@Ariznawlll

Summary

On latest main (0e8602b78), MySQL index hints are accepted but can produce incorrect results with composite secondary indexes:

  • USE INDEX(...) / FORCE INDEX FOR ORDER BY(...) / IGNORE INDEX FOR ORDER BY(...) return empty results for queries that return rows in MySQL.
  • FORCE INDEX(idx_missing) does not report the missing index. MySQL returns ERROR 1176, while MO returns 0.

Duplicate check

Searched before filing:

  • FORCE INDEX
  • USE INDEX
  • IGNORE INDEX
  • force index missing index
  • USE INDEX returns zero rows

No direct duplicate was found. Existing composite-index issues such as #25344/#25345/#25348/#25349/#25351 cover predicate/type/aggregate paths, not MySQL index-hint semantics.

Reproduce

drop database if exists mysql_compat_horizontal_index_hint;
create database mysql_compat_horizontal_index_hint;
use mysql_compat_horizontal_index_hint;

create table t (
  id int primary key,
  a varchar(8) not null,
  b int not null,
  c int not null,
  status varchar(12),
  score int,
  dt datetime,
  key idx_a_b_c_id(a,b,c,id),
  key idx_b_a_id(b,a,id),
  key idx_status_dt(status,dt,id)
);

insert into t values
(2,'g02',2,14,'archived',22,'2024-03-03 02:00:00'),
(42,'g02',5,29,'archived',58,'2024-07-15 18:00:00'),
(81,'g01',7,37,'pending',83,'2024-10-26 09:00:00'),
(82,'g02',8,44,'archived',94,'2024-11-27 10:00:00'),
(112,'g02',1,42,'active',20,'2024-05-01 16:00:00'),
(152,'g02',4,4,'active',56,'2024-09-13 08:00:00'),
(155,'g05',7,25,'hold',89,'2024-12-16 11:00:00'),
(192,'g02',7,19,'active',92,'2024-01-25 00:00:00'),
(222,'g02',0,17,'archived',14,'2024-07-27 06:00:00'),
(232,'g02',10,34,'active',23,'2024-05-09 16:00:00');

select id,a,b,c
from t use index(idx_b_a_id)
where b=7 and a in ('g01','g03','g05')
order by a,id;

select id,a,b,c
from t force index for order by(idx_a_b_c_id)
where a in ('g02','g04')
order by a,b,c,id
limit 8;

select count(*)
from t force index(idx_missing)
where a='g01';

Expected result

Matches MySQL 8.0.45:

-- USE INDEX
81 g01 7 37
155 g05 7 25

-- FORCE INDEX FOR ORDER BY
222 g02 0 17
112 g02 1 42
2   g02 2 14
152 g02 4 4
42  g02 5 29
192 g02 7 19
82  g02 8 44
232 g02 10 34

-- missing index
ERROR 1176 (42000): Key 'idx_missing' doesn't exist in table 't'

Actual result on MO

-- USE INDEX
empty result

-- FORCE INDEX FOR ORDER BY
empty result

-- missing index
0

Activity

  1. added
    kind/bug
    severity/s1
    on Jul 2, 2026
  2. added this to the MO-202607 milestone on Jul 2, 2026
  3. added theissue type on Jul 2, 2026
  4. added and removed
    severity/s1
    on Jul 2, 2026
  5. Ariznawlll commented on Jul 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    Supplemental index-hint coverage on latest main 51656deb6:

    On a composite-PK table with composite secondary indexes, hinting changes correct counts into zero. In one repro, the unhinted count happened to match MySQL, but both FORCE INDEX and IGNORE INDEX returned zero:

    Table shape:
    primary key(user_id, session_id, task_id)
    key idx_status_due_owner(status, due, user_id, session_id, task_id)
    key idx_policy_owner(policy, user_id, session_id, task_id)
    key idx_score_owner(score, user_id, session_id, task_id)
    
    where status='active'
      unhinted indexed table: count=4
      no-index control:      count=4
      force idx_status...:   count=0
      ignore idx_status...:  count=0
    
    where policy='keep'
      no-index control:      count=4
      force idx_policy...:   count=0
      ignore idx_policy...:  count=0
    
    where score=20
      no-index control:      count=1
      force idx_score...:    count=0
      ignore idx_score...:   count=0
    

    This confirms the hint issue is not limited to a simple integer-primary-key table; it also reproduces with owner-bound composite primary keys and secondary indexes that append the PK columns.

  6. added
    severity/s1
    and removed on Jul 10, 2026
  7. added
    ai-light
    on Jul 13, 2026
  8. Ariznawlll commented on Jul 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 上完成验证,并补充 motr 回归测试。

    验证环境

    • MatrixOne:main@52afd25fa63a3cb6037b7bf8ff0bf1328c4793ec
    • motr 用例:issue_25356_index_hints
    • 回归 PR:matrixorigin/motr#48

    测试过程

    使用 issue 中的复合索引表结构和数据,覆盖:

    1. USE INDEX(idx_b_a_id) + 等值/IN 条件,检查实际返回行。
    2. FORCE INDEX FOR ORDER BY(idx_a_b_c_id) + 排序和 LIMIT。
    3. IGNORE INDEX FOR ORDER BY(idx_a_b_c_id) + LIMIT/OFFSET。
    4. FORCE INDEX 下执行 GROUP BY、COUNT 和 SUM。
    5. 引用不存在的 idx_missing,要求返回 MySQL 错误 1176。

    验证结果

    • USE/FORCE/IGNORE INDEX 均返回正确数据,不再错误返回空集。
    • 排序和分页内容正确。
    • hint 下聚合结果正确。
    • 不存在的索引名正确返回 ERROR 1176: Key 'idx_missing' doesn't exist in table 't'。
    • 连续执行 5 轮均通过。
    SUMMARY | total=50 success=50 failed=0 skipped=0 | stmts=625 ok=625 fail=0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-lightkind/bugseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions