目录
示例1:
select * from a where a.id = 0 and a.field2 = 3 or id in (
select id from a where a.field3 > 3 and a.field3 < 4
)
示例2:
select * from a where a.id = 0 and a.field2 = 3 or id in (
3,4,5
)
结果:
示例中的所有字段均为索引字段
explain后 示例1子查询走了索引 外部查询扫了全表。示例2走了索引没有全表扫描
但是or换成and确可以走索引。
解决:
使用union可以解决这种问题
select id, fileds2 , fileds3 from a where fileds2 > 2 and ...
union (select id, fileds2 , fileds3 from a where fileds3 &