条件中有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
如果以%开头,索引不能用;以