暂无图片
暂无图片
8
暂无图片
暂无图片
暂无图片

多表关联连接顺序是怎样的

原创 陈龙 2019-03-21
875

问题描述

测试数据创建

drop table test_tab1 ;
 create table test_tab1 as select  /*+ordered*/ * from all_objects,(select * from dual connect by level < 5)   ;
 
drop table test_tab2;
create table test_tab2 as select * from test_tab1 where rownum < 1e4;
 
analyze table test_tab1 compute statistics for table for all indexes for all columns;
analyze table test_tab2 compute statistics for table for all indexes for all columns;

对于以下SQL, 列出可能的连接顺序

select count(*) from test_tab1 a , test_tab2 b, test_tab2 c
where a.object_id = b.object_id and a.object_name = c.object_name

专家解答

image.png

test_tab1 共有284,408 行记录

test_tab2 共有9,999行记录

  

select /*+ test 8*/ count(*)
from test_tab1 a,
     test_tab2 b,
     test_tab2 c
where a.object_id = b.object_id
  and a.object_name = c.object_name

test_tab1 a 作为驱动表,test_tab2 b c 作为被驱动表。

先对表test_tab1 a ,test_tab2  c 全表扫描object_id 相同的数量,两表返回了39996 行记录,在于test_tab2 b的object_name 做hash join 连接。

image.png

即使调整where 连接顺序,依旧按这个顺序。

最后修改时间:2019-04-25 17:39:42
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论