mysql索引命中规则

 

多列索引 遵循:最左匹配原则

ALTER TABLE student ADD INDEX userName_age_phone_index (userName,age,phone);

这种创建索引的方式在多项查找时要优于单一索引,由于mysql的采用的b+树方式,因此不在需要扫描任何记录,直接就可以得到满足需求的结果集。而这种方式其实相当于创建了三个索引(userName),(userName,age),(userName,age,phone)。

因此在当进行以下查询时仍会命中mysql索引

select * from student where userName=‘小明’;

select * from student where userName=‘小明’ and age=19;

select * from student where userName=‘小明’ and age=19 and phone=‘1887821’;

不会命中索引的情况

1.如果where后面有or,这样不会命中索引如:

select * from student where userName=‘小明’ or age=19;

如果想要or后面的也走索引的话,应该是创建两个单列索引

2.like是以%开头的不会命中索引如:

select * from student where userName like %明

3.如果列类型是字符串,那一定要在条件中将数据使用引号引用起来,否则不使用索引如:

select * from student where userName=‘小明’ and age=19 and phone=1887821 ;

phone 定义的数据格式是字符串,但是sql中使用的整型

4.没有查询条件,或者查询条件没有建立索引

5.查询条件中,在索引列上使用函数(+/-*/)

select * from student where userName=‘小明’ and age-1=19 错误

select * from student where userName=‘小明’ and age=20 正确

采用 not in, not exist,!=, <> , is null , is not null 不会命中索引

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值