ITPub博客

首页 > 数据库 > Oracle > 关于表联结方法(一)

关于表联结方法(一)

Oracle 作者:dbhelper 时间:2014-11-26 10:51:52 0 删除 编辑
在sql语句中,如果from后面有多个表时,关于表的联结方法是很重要的一个环节。
大体有nested loop join,hash join,merge join

-->nested loop join

这种场景一般适用于大表和小表的关联,一般来说小表适用做为驱动表,对于小表中的匹配记录和大表做关联,这个时候小表是在外部循环,大表在内部循环,小表中的匹配记录都和大表做一个关联。
SQL> create table t as select *from dba_objects where object_id is not null;
SQL> create table t1 as select *from user_objects where object_id is not null;
SQL> exec dbms_stats.gather_table_stats(user,'T',cascade=>TRUE);
SQL> exec dbms_stats.gather_table_stats(user,'T1',cascade=>TRUE);
SQL> create unique index ind_t on t(object_id);
SQL> create unique index int_t1 on t1(object_id) ;
SQL> select  count(*)from t;
  COUNT(*)
----------
     10783
下面的例子,t1表中的数据较少,表t中的数据多,就以表t1位驱动表,走了全索引扫描查取到t1的数据,然后对于t1中的数据和t做匹配,匹配的时候走了唯一性扫描


-->hash join
这种场景适合于大表和大表之间的关联。通过hash算法来做两个表之间的匹配映射。

SQL> create table t as select *from dba_objects where object_id is not null;
SQL> create table t1 as select *from dba_objects where object_id is not null;
SQL> exec dbms_stats.gather_table_stats(user,'T',cascade=>TRUE);
SQL> exec dbms_stats.gather_table_stats(user,'T1',cascade=>TRUE);
SQL> create unique index ind_t on t(object_id);
SQL> create unique index int_t1 on t1(object_id) ;
SQL> select  count(*)from t;
  COUNT(*)
----------
     10783
对于表t1中的记录,都是通过hash映射来匹配t中的记录。可能对于cpu的资源消耗还是相对较多的,因为内部做了大量的计算。在生产环境中的实践来说,hash join还是不错的。特别是在和并行结合之后。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8494287/viewspace-1346959/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-10-14

  • 博文量
    651
  • 访问量
    392140