先来列举两个sql
select *
from am_base_info a, am_talent_info t
where a.id_num = t.id_num
and t.talent_level = '1'
and (t.highend_type = '1' or t.highend_type = '3' or
t.highend_type = '4')
or a.region_code in (110001)
通过oracle中的explain查看执行计划
通过执行计划我们可以看到,是通过扫描两个表然后使用笛卡尔积,最后再根据其他条件进行查询合并,这就相当耗费时间了,这里主要是在于最后一个or,个人理解相当于查询结果和and查询的结果合并
那么我们再来看另一个查询
select *
from am_base_info a, am_talent_info t
where a.id_num = t.id_num
and t.talent_level = '1'
and (t.highend_type = '1' or t.highend_type = '3' or
t.highend_type = '4')
and (a.region_code in (110001))
这个结果和上面的对比一下就能看到 我们只查询了次循环,结果就出来了,而没有上面的笛卡尔积,为什么呢?这是因为最后一个and中,把or给抛弃掉了,就是说我们在上面的and的查询结果集,判断符合最后一个and的结果,数据就可以出来了,而不需要再次扫描,合并结果集
参考:链接