索引失效
注:索引列无法存储null值,与null值有关的操作都不会使用索引
- 语句中有or,且or的条件中有非索引字段,索引失效
例如:
id为索引列,name字段未建索引,以下语句不使用索引
selsect * from A where id=1 or name="test"
- 符合索引中未用最左列主索引字段
例如,建索引时(id,name,number)
使用时,不使用id,则不会使用索引
select * from A where name=“test” and number=123
- like前缀匹配,索引失效
例如:
select * from A where name like “%test”
- 条件查询中做了隐式数据转换,索引失效
例如:
id本是integer型字段,条件中做varcgar类型条件查询,sql引擎会转换类型,但是索引失效。注:sql引擎做类型转换速度很慢
select * from A where id=“123”
- where子句中对索引列有数学运算,如abs等,索引失效
select * from A where id=id+1
- where子句中有对索引列使用函数,索引失效
select * from A where ABS(id)=1
- sql引擎人为使用全表扫描比使用索引查询快,则会放弃扫描
例如:数据量极小的表