嵌套循环连接(nested loops join)

访问次数:驱动表返回几条,被驱动表访问多少次。

驱动表是否有顺序:有。

是否要排序:否。

   应用场景:  1.关联中有一个表比较小;

2.被关联表的关联字段上有索引;

3.索引的键值不应该重复率很高。

如果你做过开发,就把它看成两层嵌套的for循环。

下面我们来做个实验:

SQL> create table test1 as select * from dba_objects where rownum <=100;

SQL> create table test2 as select * from dba_objects where rownum <=1000;
SQL> exec dbms_stats.gather_table_stats(user,'test1');

SQL> exec dbms_stats.gather_table_stats(user,'test2');

SQL> alter session set statistics_level=all;

SQL> select /*+leading(t1) use_nl(t2)*/count(*)
  2    from test1 t1, test2 t2
  3   where t1.object_id = t2.object_id;

  COUNT(*)

----------

       100

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

PLAN_TABLE_OUTPUT

---------------------------------------------------------------------------------------------------------

SQL_ID  3v5gu7ppdsz67, child number 0

-------------------------------------

select /*+leading(t1) use_nl(t2)*/count(*)   from test1 t1, test2 t2  where

t1.object_id = t2.object_id

Plan hash value: 1459699139

----解释一下:

Starts为该sql执行的次数。

E-Rows为执行计划预计的行数。

A-Rows为实际返回的行数。A-Rows跟E-Rows做比较,就可以确定哪一步执行计划出了问题。
A-Time为每一步实际执行的时间(HH:MM:SS.FF),根据这一行可以知道该sql耗时在了哪个地方。
Buffers为每一步实际执行的逻辑读或一致性读。
Reads为物理读。
OMem、1Mem为执行所需的内存评估值,0Mem为最优执行模式所需内存的评估值,1Mem为one-pass模式所需内存的评估值。
0/1/M 为最优/one-pass/multipass执行的次数。
Used-Mem耗的内存

---------------------------------------------------------------------------------------

| Id  | Operation           | Name  | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

---------------------------------------------------------------------------------------

|   1 |  SORT AGGREGATE     |       |      1 |      1 |      1 |00:00:00.01 |    1504 |

|   2 |   NESTED LOOPS      |       |      1 |    100 |    100 |00:00:00.01 |    1504 |

|   3 |    TABLE ACCESS FULL| TEST1 |      
|    100 |    100 |00:00:00.01 |       4 |

|*  4 |    TABLE ACCESS FULL| TEST2 |    
|      1 |    100 |00:00:00.01 |    1500 |

---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   4 - filter("T1"."OBJECT_ID"="T2"."OBJECT_ID")

SQL> select /*+leading(t1) use_nl(t2)*/count(*)
  2    from test1 t1, test2 t2
  3   where t1.object_id = t2.object_id
  4     and t1.object_id in (10, 11, 12);

  COUNT(*)

----------

         3

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

PLAN_TABLE_OUTPUT

---------------------------------------------------------------------------------------------------------

SQL_ID  0skx6hyjtsncu, child number 0

-------------------------------------

select /*+leading(t1) use_nl(t2)*/count(*)   from test1 t1, test2 t2  where

t1.object_id = t2.object_id    and t1.object_id in (10, 11, 12)

---------------------------------------------------------------------------------------

| Id  | Operation           | Name  | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

---------------------------------------------------------------------------------------

|   1 |  SORT AGGREGATE     |       |      1 |      1 |      1 |00:00:00.01 |      49 |

|   2 |   NESTED LOOPS      |       |      1 |      3 |      3 |00:00:00.01 |      49 |

|*  3 |    TABLE ACCESS FULL| TEST1 |      
|      3 |      3 |00:00:00.01 |       4 |

|*  4 |    TABLE ACCESS FULL| TEST2 |      
|      1 |      3 |00:00:00.01 |      45 |

---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   3 - filter(("T1"."OBJECT_ID"=10 OR "T1"."OBJECT_ID"=11 OR

              "T1"."OBJECT_ID"=12))

   4 - filter((INTERNAL_FUNCTION("T2"."OBJECT_ID") AND

              "T1"."OBJECT_ID"="T2"."OBJECT_ID"))

SQL> select /*+leading(t1) use_nl(t2)*/count(*)
  2    from test1 t1, test2 t2
  3   where t1.object_id = t2.object_id
  4     and t1.object_id =10;

  COUNT(*)

----------

         1

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

PLAN_TABLE_OUTPUT

---------------------------------------------------------------------------------------------------------

SQL_ID  24g0zhvczyf2h, child number 0

-------------------------------------

select /*+leading(t1) use_nl(t2)*/count(*)   from test1 t1, test2 t2  where

t1.object_id = t2.object_id    and t1.object_id =10

Plan hash value: 1459699139

---------------------------------------------------------------------------------------

| Id  | Operation           | Name  | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

---------------------------------------------------------------------------------------

|   1 |  SORT AGGREGATE     |       |      1 |      1 |      1 |00:00:00.01 |      19 |

|   2 |   NESTED LOOPS      |       |      1 |      1 |      1 |00:00:00.01 |      19 |

|*  3 |    TABLE ACCESS FULL| TEST1 |      
|      1 |      1 |00:00:00.01 |       4 |

|*  4 |    TABLE ACCESS FULL| TEST2 |      
|      1 |      1 |00:00:00.01 |      15 |

---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   3 - filter("T1"."OBJECT_ID"=10)

   4 - filter("T2"."OBJECT_ID"=10)

嵌套循环连接(nested loops join)原理的更多相关文章

  1. 嵌套循环连接(Nested Loops Joins)

    The nested loops join, also called nested iteration, uses one join input as the outer input table(sh ...

  2. SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join

    nested loops join(嵌套循环)   驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...

  3. Nested Loops join时显示no join predicate原因分析以及解决办法

    本文出处:http://www.cnblogs.com/wy123/p/6238844.html 最近遇到一个存储过程在某些特殊的情况下,效率极其低效, 至于底下到什么程度我现在都没有一个确切的数据, ...

  4. Merge join、Hash join、Nested loop join对比分析

    简介 我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge Join,Hash Join ...

  5. 浅谈SQL Server中的三种物理连接操作(Nested Loop Join、Merge Join、Hash Join)

    简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...

  6. join中级篇---------hash join & merge join & nested loop Join

    嵌套循环连接(Nested Loop Join) 循环嵌套连接是最基本的连接,正如其名所示那样,需要进行循环嵌套,嵌套循环是三种方式中唯一支持不等式连接的方式,这种连接方式的过程可以简单的用下图展示: ...

  7. 三大表连接方式详解之Nested loop join和 Sort merge join

    在早期版本,Oracle提供的是nested-loop join,两表连接就相当于二重循环,假定两表分别有m行和n行       如果内循环是全表扫描,时间复杂度就是O(m*n)       如果内循 ...

  8. oracle多表连接方式Hash Join Nested Loop Join Merge Join

    在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式:        嵌套循环(Nested  Loops (NL) ...

  9. oracle表连接------&gt;排序合并连接(Merge Sort Join)

    排序合并连接 (Sort Merge Join)是一种两个表在做连接时用排序操作(Sort)和合并操作(Merge)来得到连接结果集的连接方法. 对于排序合并连接的优缺点及适用场景例如以下: a,通常 ...

随机推荐

  1. Codeforces Round #279 (Div. 2) B - Queue 水题

    #include<iostream> #include<mem.h> using namespace std; ],q[]; int main() { int n,x,y; m ...

  2. Python学习笔记(三):随机生成函数方法

    本文是在Python2下总结! Python中的random模块用于生成随机数,如果想生成随机数需要先导入random的模块然后才能使用其中的方法,下面简单介绍一下常用的结果函数方法: 1·.rand ...

  3. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

  4. [Node.js]NET模块

    摘要 net模块提供了一些用于底层的网络通信的小工具,包含了创建服务器和客户端的方法.可以使用该模块模拟请求等操作. net模块 引入net模块 var net=require("net&q ...

  5. IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解

    copy与retain的区别: copy是创建一个新对象,retain是创建一个指针,引用对象计数加1.Copy属性表示两个对象内容相同,新的对象retain为1 ,与旧有对象的引用计数无关,旧有对象 ...

  6. C#编程(二)

    C#中的变量 例如:int i;//声明一个int类型的变量,变量名是 i;在未为该变量进行赋值操作前,禁止使用该变量.使用(=)给变量赋值,在声明之后可以 i=10来赋值.也可以在声明一个变量的同时 ...

  7. 【centOS】centos7 查看和关闭防火墙

    查看防火墙状态 firewall-cmd --state running代表防火墙正在运行 停止firewall systemctl stop firewalld.service 禁止firewall ...

  8. 【IDEA】【maven】idea使用maven插件 打包提示找不到符号找不到类,但是却没有错误

    [本篇方法如果无效,请使用终极解决方法] [终极解决方法]:https://www.cnblogs.com/sxdcgaq8080/p/10117852.html 如下,在右侧maven工具中进行打包 ...

  9. Android平台上优秀的开源项目

    软件名:gaeproxy 软件作用:Android手机配置GoAgent. 项目地址:https://github.com/madeye/gaeproxy.git 软件名:ProxyDroid 软件作 ...

  10. 关于查询排序DTO的封装

    DTO: public class SortDto { //排序方式 private String orderType; //排序字段 private String orderField; publi ...