条件中有or
- 如果条件中有or,其中一个字段是索引字段,另一个是非索引字段,此时索引不会被使用,全表扫描。
- 如果条件中有or,左右是两个不同的独立索引字段,此时两个索引字段都会用。
但其实完全可以用union来代替:
select * from xd_student where id = 2
union
select * from xd_student where room = 401
- 如果条件中有or,用的都是同一个索引字段,索引有用。
但其实完全可以用in来代替or:
select * from xd_student where id in (2,4);
使用like
如果以%开头,索引不能用;以%结束,索引能用但是不用。
列类型是字符串,没有使用引号
只有加链引号才会用索引。
索引字段表达式
【补充】
查看索引的使用情况
show status like ‘Handler_read%’;
handler_read_key:这个值越大说明使用索引查询到的次数越多
handler_read_rnd_next:这个值越高,说明查询低效