首页 > 解决方案 > LIKE 查询没有禁用 MySQL 中的复合索引的原因是什么

问题描述

这是我的表结构

CREATE TABLE t(
c1 CHAR(1) not null,
c2 CHAR(1) not null,
c3 CHAR(1) not null,
c4 CHAR(1) not null,
c5 CHAR(1) not null
)ENGINE InnoDB CHARSET UTF8;

alter table t add index c1234(c1,c2,c3,c4);

当我使用explain select * from t where c1='1' and c2 >'2' and c3='4'时,解释结果显示key_len只有 6,即只有 c1 和 c2 处于活动状态。

但是,执行后explain select * from t where c1='1' and c2 like '2%' and c3='4',结果显示key_len为 9,所有索引都处于活动状态。

我认为LIKE %查询是一个范围查询,例如<and >,而 的出现LIKE %并没有影响它之后索引的使用。

标签: mysqlb-tree-index

解决方案


推荐阅读