1,708 字 · 约 6 分钟读完
概念
索引失效指的是 SQL 的写法让 MySQL 无法高效利用已有索引,退化为部分使用索引或全表扫描。失效并不总是表现为执行计划里 key = NULL,更常见的是只用到联合索引的前几列。
判断索引是否被有效利用,主要看 EXPLAIN 输出中的几个字段:
| 字段 | 作用 |
|---|---|
type | 访问类型,从好到差大致是 system / const / eq_ref / ref / range / index / ALL |
key | 实际使用的索引名,未使用索引时为 NULL |
key_len | 实际使用的索引长度,长度越长说明命中的索引列越多 |
Extra | 额外信息,Using where 表示在存储层回表后过滤,Using index condition 表示使用了索引下推 |
下面以 tb_seller 表和联合索引 (name, status, address) 为例,逐一说明。
示例表与索引
CREATE TABLE tb_seller (
sellerid varchar(100) PRIMARY KEY,
name varchar(100),
nickname varchar(100),
password varchar(100),
status char(1),
address varchar(100),
createtime datetime
);
create index tb_seller_index on tb_seller(name, status, address);
索引顺序如下:
| Key_name | Seq_in_index | Column_name |
|---|---|---|
tb_seller_index | 1 | name |
tb_seller_index | 2 | status |
tb_seller_index | 3 | address |
后面所有判断都以这个联合索引顺序为前提。
违反最左前缀法则
联合索引的命中必须从最左列开始,跳过最左列就无法整体使用该索引。
反例,跳过 name:
select * from tb_seller where status = '1';
select * from tb_seller where status = '1' and address = '北京市';
近似执行计划:
| SQL | type | key | key_len | Extra | 说明 |
|---|---|---|---|---|---|
where status='1' | ALL | NULL | NULL | Using where | 联合索引整体失效 |
where status='1' and address='北京市' | ALL | NULL | NULL | Using where | 仍跳过 name |
正例,从 name 开始:
select * from tb_seller where name = '小米科技';
select * from tb_seller where name = '小米科技' and status = '1';
近似执行计划:
| SQL | type | key | key_len | Extra | 说明 |
|---|---|---|---|---|---|
where name='小米科技' | ref | tb_seller_index | 较短 | Using index condition | 命中最左列 |
where name='小米科技' and status='1' | ref | tb_seller_index | 更长 | Using index condition | 使用前两列 |
实际业务中,如果后台筛选页经常按「名称」「名称 + 状态」「名称 + 状态 + 城市」组合来查询,那么 (name, status, address) 这个索引设计就和查询模式匹配。如果主查询经常只有「状态」或「城市」,则索引本身需要调整。
中间列断开,后续列用不上
命中了最左列但跳过中间列时,联合索引只能用到断开之前的那一部分。
select * from tb_seller
where name = '小米科技' and address = '北京市';
这里命中了 name,但跳过了 status,所以 address 通常无法继续利用该联合索引:
| SQL | type | key | key_len | Extra | 说明 |
|---|---|---|---|---|---|
name='小米科技' and address='北京市' | ref | tb_seller_index | 只到 name 的长度 | Using index condition | 后续列无法连续命中 |
对比连续命中:
select * from tb_seller
where name = '小米科技' and status = '1' and address = '北京市';
flowchart LR
A["联合索引顺序: name -> status -> address"] --> B["SQL: name + status + address"]
B --> C["连续命中: 三列都用上"]
A --> D["SQL: name + address"]
D --> E["中间断开 status"]
E --> F["address 无法继续利用联合索引"]
范围查询右侧的列无法继续定位
当中间列变成范围查询,范围条件右侧的列通常不能继续用于索引定位。
-- 全部等值,三列都能命中
select * from tb_seller
where name = '小米科技' and status = '1' and address = '北京市';
-- 中间变成范围,address 通常无法继续定位
select * from tb_seller
where name = '小米科技' and status > '1' and address = '北京市';
| SQL | type | key | key_len | Extra | 说明 |
|---|---|---|---|---|---|
name='A' and status='1' and address='BJ' | ref | tb_seller_index | 更长 | 较干净 | 三列连续等值 |
name='A' and status>'1' and address='BJ' | range | tb_seller_index | 只到范围列附近 | Using index condition | address 不再参与定位 |
准确的表述是:范围查询前面的列还能用索引,范围查询右边的列通常无法继续利用联合索引。
在索引列上做函数、计算、表达式操作
对索引列做函数处理会让 MySQL 无法直接用原始索引值定位,只能退回到逐行计算。
select * from tb_seller where substring(name, 3, 2) = '科技';
select * from tb_seller where left(name, 2) = '小米';
| SQL | type | key | Extra | 说明 |
|---|---|---|---|---|
substring(name,3,2)='科技' | ALL | NULL | Using where | 索引列被函数处理 |
更稳妥的写法是把计算移到等号右边,或者直接传完整值:
select * from tb_seller where name like '小米%';
select * from tb_seller where name = '小米科技';
业务里常见的情形是列表页模糊筛选时把字段直接包进函数,或者报表 SQL 为了格式化时间对索引列做 date()、substring() 等处理。
字符串不加单引号,触发隐式类型转换
当字符串列与数字常量比较时,MySQL 可能做隐式类型转换,索引利用效果会下降。
-- 类型一致
select * from tb_seller where name = '科技' and status = '0';
-- status 是字符类型,但写成数字
select * from tb_seller where name = '科技' and status = 0;
| SQL | key | key_len | Extra | 说明 |
|---|---|---|---|---|
status='0' | tb_seller_index | 更长 | 正常 | 类型一致 |
status=0 | 效果可能变差 | 可能变短 | Using index condition | 可能触发隐式类型转换 |
需要注意:并不是所有不加引号的情形都一定导致索引失效,而是类型不一致时,转换开销可能让优化器放弃使用索引。
LIKE 以 % 开头
LIKE 的索引可用性取决于前缀是否固定。
-- 前缀不固定,容易失效
select sellerid, name from tb_seller where name like '%黑马程序员%';
select sellerid, name from tb_seller where name like '%黑马程序员';
-- 前缀固定,通常可走索引
select sellerid, name from tb_seller where name like '黑马程序员%';
| SQL | type | key | Extra | 说明 |
|---|---|---|---|---|
like '%黑马程序员%' | ALL | NULL | Using where | 前缀不固定 |
like '%黑马程序员' | ALL | NULL | Using where | 仍是前缀不固定 |
like '黑马程序员%' | range | tb_seller_index | Using index condition | 固定前缀 |
flowchart LR
A["name 索引按字典序有序存储"] --> B["like '黑马%'"]
B --> C["前缀固定 -> 可定位范围 -> 索引可用"]
A --> D["like '%黑马' 或 '%黑马%'"]
D --> E["前缀不固定 -> 无法从左定位 -> 索引容易失效"]
索引失效场景总表
| 场景 | 容易失效的写法 | 核心原因 |
|---|---|---|
| 最左前缀 | where status='1' | 跳过联合索引最左列 |
| 中间断开 | where name='A' and address='BJ' | 联合索引不能跨列连续命中 |
| 范围查询 | name='A' and status>'1' and address='BJ' | 范围列右侧列通常无法继续定位 |
| 函数或运算 | where substring(name,3,2)='科技' | 对索引列做计算 |
| 隐式转换 | status=0(列实际为字符串) | 类型不一致,可能触发转换 |
LIKE 模糊查询 | like '%黑马%' | 前缀不固定,无法有序匹配 |
定位这类问题时,通常先用 EXPLAIN 看 type、key、key_len、Extra 这四个字段,再结合 SQL 写法判断是完全没走索引,还是只用了联合索引的前几列。