表的链接

三大类型连接

          1)嵌套循环连接(Nested loops join)

          2)哈希连接(hash join)

          3) 排序合并连接(merge sort join)


1:各类连接访问次数

    1)嵌套循环连接

    create table join_01_objects as select * from dba_objects where rownum < 1000 ;

    create table join_05_objects as select * from dba_objects where rownum < 1000 ;

    insert into join_05_objects select * from dba_objects where rownum < 1000;

    insert into join_05_objects select * from dba_objects where rownum < 1000;

    insert into join_05_objects select * from dba_objects where rownum < 1000;

    insert into join_05_objects select * from dba_objects where rownum < 1000;

    alter session set statistics_level=all;

    select /*+ use_nl(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id ;


    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));


    select /*+ use_nl(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id in (1050,1060);

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ use_nl(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ use_nl(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=99999 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select count(1) from join_01_objects t1;

    select count(1) from join_01_objects t1 where t1.object_id in (1050,1060);

    select count(1) from join_01_objects t1 where t1.object_id=1050 ;

    select count(1) from join_01_objects t1 where t1.object_id=99999;

  嵌套循环连接中,驱动表返回多少条记录,被驱动表就被访问多少次。


  2) 哈希连接表访问次数

    select /*+ use_hash(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ use_hash(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id in (1050,1060);

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ use_hash(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ use_hash(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=99999 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    在HASH连接中,驱动表和被驱动表都只会访问0次/1次

    被驱动表访问0次好理解,驱动表什么情况下访问0次


    select /*+ use_hash(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=99999 and 1=2 ;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    3) 排序合并的表访问次数


    select /*+ use_merge(t1,t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id in (1040,1050,1060)

    and 1=2 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));


    在排序合并连接中,表都只会访问0次/1次

    排序合并连接没有驱动表和被驱动表的概念,而嵌套循循环和哈希连接需要考虑驱动表和被驱动表的情况

2: 各类连接驱动顺序区别


    1) 嵌套循环连接驱动表的顺序

    select /*+ leading(t1)use_nl(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t2)use_nl(t1)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    嵌套循环连接要特别注意驱动表的顺序,小的结果集先访问,大的结果结后访问


    2)哈希连接的表驱动顺序

    select /*+ leading(t1)use_hash(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t2)use_hash(t1)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

  哈希连接要特别注意驱动表的顺序,小的结果集先访问,大的结果结后访问

    3)排序合并连接

    select /*+ leading(t1)use_merge(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t2)use_merge(t1)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));


    排序合并连接没有驱动表的概念,无论哪张表在前面都无所谓。

3:各类连接排序情况

    1)除嵌套循环连接都需要排序

    2)排序只取部分字段

      哈希连接并不排序,消耗的内存是用于建立哈希表

      哈希连接、排序合并连接的简单优化思想:不要取多余的字段参与排序


    select /*+ leading(t1)use_hash(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));


    select /*+ leading(t1)use_merge(t2)*/ t1.object_id,t1.STATUS,t2.object_id,t2.status

    from join_01_objects t1,join_05_objects t2

    where t1.object_id=t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    排序合并连接自行对比

4: 各类连接限制场景对比

    1)哈希连接的限制

    select /*+ leading(t1)use_hash(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id <> t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t1)use_hash(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id like t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t1)use_hash(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id < t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    哈希连接不支持不等值连接<>,不支持>和<的连接方式,也不支持LIKE的连接方式


    2)排序合并的限制

    select /*+ leading(t1)use_merge(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id <> t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t1)use_merge(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id like t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    select /*+ leading(t1)use_merge(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id < t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));   

    排序合并连接不支持不等值连接<>,也不支持LIKE的连接方式


    3)循环嵌套无限制

5: 表连接综合运用 

    1)嵌套循环连接与索引

    select /*+ leading(t1) use_nl(t2) */ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id = t2.object_id

    and t1.OBJECT_NAME='JIJOIN$' ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));   


    t1.object_id < t2.object_id 是连接条件,

    t1.OBJECT_NAME='JIJOIN$' ;  是限制条件

    create index join_01_index on join_01_objects(OBJECT_NAME);

    create index join_05_index on join_05_objects(object_id);

    驱动表的限制条件所在列有索引

    被驱动表的连接条件所在列有索引


    嵌套循环连接使用总结:

    1:两表关联返回的记录不多,驱动表结果集合返回少量条数

    2:遇到不等值查询,导致哈希和排序合并不能使用

    3:索引使用思想

      驱动表的限制条件列有索引

      被驱动表的连接条件所在列有索引

    2)哈希连接与索引

      起不到传递作用,与单表索引类似

      HASH_AREA_SIZE


    3)排序合并连接与索引


    drop index join_01_index;

    drop index join_05_index;

    select /*+ ordered use_merge(t2)*/ *

    from join_01_objects t1,join_05_objects t2

    where  t1.object_id = t2.object_id

    and t1.object_id=1050 ;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));



    select /*+ ordered use_merge(t2)*/ t1.object_id,t2.object_id

    from join_01_objects t1,join_05_objects t2 ;

    where  t1.object_id = t2.object_id  order by 1;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));



    create index join_01_index on join_01_objects(object_id);

    create index join_05_index on join_05_objects(object_id);


    select /*+ ordered use_merge(t2)*/ t1.object_id,t2.object_id

    from join_01_objects t1,join_05_objects t2 ;

    where  t1.object_id = t2.object_id  order by 1;

    alter session set statistics_level=all;

    select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

    连接条件建索引后,merge sort join 的排序减少一次

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,539评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,911评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,337评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,723评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,795评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,762评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,742评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,508评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,954评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,247评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,404评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,104评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,736评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,352评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,557评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,371评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,292评论 2 352

推荐阅读更多精彩内容