1.最左匹配原则
在使用联合索引时要满足最左匹配原则。
--给表创建联合索引
CREATE INDEX user on user_innodb(name, age, address)
不满足最左匹配原则的情况1
select * from user_innodb where age = 20
原因:
不满足最左,未使用索引name
不满足最左匹配原则的情况2
select * from user_innodb where name = "张三" and address = "北京"
原因:
满足最左,但中间跳过了索引age,只会使用到索引列name,后面的索引列都会失效。
不满足最左匹配原则的情况3
select * from user_innodb where substring(name,0,2) = "zs"
原因:
索引上不能使用运算操作
2.其它
1.使用了or,索引失效
select * from user_innodb where name = "张三" or name = "李四"
2.以%开头的like模糊查询,索引失效
select * from user_innodb where name like "%三"