SQL优化-关联子查询改表连接

1.数据脚本

drop table test1 ;

create table test1 as

  select * from (select d.*,rownum as rn

                 from dba_objects d

                             where d.owner not in ('SYS','SYSTEM','PUBLIC'))

              where mod(rn,20)=0;

alter table test1 add constraint pk_test1 primary key (object_id);

2.SQL优化

2.1 优化前

select t1.*

from test1 t1

where t1.object_id in (select t2.object_id

                       from test2 t2

                                      where t2.rn in (select t3.rn

                                                      from test3 t3

                                                                  where t1.object_name=t3.object_name

                                                                    and t3.data_object_id=t2.data_object_id));

SQL优化前,优化器还是很智能的进行为此下推,讲关联子查询改成了表连接。虽然性能不错,我们还是要根据SQL的执行原理进行下一步优化。

2.2 关联子查询改为表连接

select t1.*

from test1 t1

    ,test2 t2

       ,test3 t3

where t1.object_id=t2.object_id

  and t3.rn=t2.rn

  and t2.data_object_id=t3.data_object_id

and t1.object_name=t3.object_name;

SQL用来从表中一层层的筛选数据,我们可以把in条件统一改成表连接,这样,执行计划走了Hash连接,性能还是有很大提高。

这里,再次强调,SQL处理是为了根据条件一层层筛选数据,不同意其他高级语言的流程式处理。

发布了51 篇原创文章 · 获赞 4 · 访问量 4224

猜你喜欢

转载自blog.csdn.net/songjian1104/article/details/102898036