索引失效
隐式转换
什么是隐式转换?


运行 select '10'>9;返回 1
运行 select '10'>'9';返回 0
这就是隐式转换规则,对于不同类型的值比较,会把字符串转换为数字
create table db.test(
id BIGINT auto_increment PRIMARY key,
name varchar(30),
create_time TIMESTAMP(6),
update_time TIMESTAMP(6) on UPDATE CURRENT_TIMESTAMP(6)
);
create index idx_name on db.test(name);

select * from test where name = 1 不走索引

select * from test where name = '1' 走索引
等价于 select * from test where name = cast('1' as signed int);
这是因为 mysql 在遇到字符串和数字比较时,会把字符串转换为数字。
select * from test where name = 1; 等价于 select * from test where cast(name as signed int)=1;
对索引使用函数

select * from test where length(name)=2;
对索引使用函数不走索引。
因为索引保存的是索引字段的原始值,而不是经过函数计算后的值。
WHERE 子句中的 OR

因为 OR 的含义是两个条件满足一个即可,所以只有一个条件列是索引列是没意义的