上一篇解析链接如下:

https://www.cnblogs.com/wcwen1990/p/9325968.html

1、SQL示例1:

SQL> select *

from (

select * from tmp1 where c >= 1

) t1 left join (

select * from tmp2 where b < 30

) t2 on t1.a = t2.a

and t2.d > 1 and t1.e >= 2

where t1.b < 50

;

A        B           C      E         A        B       D          E

---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
      2       20           2      2         2           20       2          2
      4       40           4      4
      3       30           3      3
      1       10           1      1

Execution Plan

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

Plan hash value: 2592321047

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

| Id  | Operation       | Name | Rows  | Bytes | Cost (%CPU)| Time      |

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

|   0 | SELECT STATEMENT   |      |    4 |   416 |    7  (15)| 00:00:01 |

|*  1 |  HASH JOIN OUTER   |      |    4 |   416 |    7  (15)| 00:00:01 |

|*  2 |   TABLE ACCESS FULL| TMP1 |    4 |   208 |    3   (0)| 00:00:01 |

|*  3 |   TABLE ACCESS FULL| TMP2 |    1 |    52 |    3   (0)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

1 - access("TMP1"."A"="TMP2"."A"(+))
        filter("TMP1"."E">=CASE    WHEN ("TMP2"."A"(+) IS NOT NULL) THEN 2
           ELSE 2 END )
    2 - filter("TMP1"."B"<50 AND "C">=1)
    3 - filter("TMP2"."D"(+)>1 AND "B"(+)<30)

Note

-----
    - dynamic sampling used for this statement (level=2)

Statistics

----------------------------------------------------------
       0  recursive calls
       0  db block gets
       7  consistent gets
       0  physical reads
       0  redo size
        1082  bytes sent via SQL*Net to client
     524  bytes received via SQL*Net from client
       2  SQL*Net roundtrips to/from client
       0  sorts (memory)
       0  sorts (disk)
       4  rows processed

postgres=# explain analyze select *

postgres-# from (

postgres(# select * from tmp1 where c >= 1

postgres(# ) t1 left join (

postgres(# select * from tmp2 where b < 30

postgres(# ) t2 on t1.a = t2.a

postgres-# and t2.d > 1 and t1.e >= 2

postgres-# where t1.b < 50

postgres-# ;
                                                   QUERY PLAN                                                 

--------------------------------------------------------------------------------------------------------------
  Hash Left Join  (cost=34.90..80.00 rows=181 width=32) (actual time=0.021..0.035 rows=4 loops=1)
    Hash Cond: ("outer".a = "inner".a)
    Join Filter: ("outer".e >= 2)
    ->  Seq Scan on tmp1  (cost=0.00..34.45 rows=181 width=16) (actual time=0.006..0.011 rows=4 loops=1)
          Filter: ((c >= 1) AND (b < 50))
    ->  Hash  (cost=34.45..34.45 rows=181 width=16) (actual time=0.007..0.007 rows=1 loops=1)
          ->  Seq Scan on tmp2  (cost=0.00..34.45 rows=181 width=16) (actual time=0.002..0.003 rows=1 loops=1)
                Filter: ((b < 30) AND (d > 1))
  Total runtime: 0.063 ms

(9 rows)

SQL执行计划的分析:

1) 全表扫描左表TMP1,同时根据TMP1表子查询条件"C">=1和where过滤条件"T1"."B"<50联合过滤,即filter("TMP1"."B"<50 AND "C">=1),计算结果临时表记为tmp1;

2) 全表扫描右表TMP2,同时根据TMP2表子查询条件"B"(+)<30和on子句"T2"."D"(+)>1联合过滤,即filter("TMP2"."D"(+)>1 AND "B"(+)<30),计算结果临时表记为tmp2;

3) 左表TMP1及右表TMP2处理后临时表tmp1和tmp2通过access("TMP1"."A"="TMP2"."A"(+))连接条件进行Hash Left Join操作,左临时表结果集全量返回,右表不匹配行置为null,返回结果临时表记为tmp3;

4) 返回结果集。

2、SQL示例2:

SQL> select *

from (

select * from tmp1 where c >= 1

) t1 left join (

select * from tmp2 where b < 30

) t2 on t1.a = t2.a

and t2.d > 1 and t1.e >= 2

where t1.b < 50 and t2.e <= 3

;

A        B           C      E         A        B       D          E

---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
      2       20           2      2         2           20       2          2

Execution Plan

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

Plan hash value: 1630095649

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

| Id  | Operation       | Name | Rows  | Bytes | Cost (%CPU)| Time      |

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

|   0 | SELECT STATEMENT   |      |    1 |   104 |    7  (15)| 00:00:01 |

|*  1 |  HASH JOIN       |      |    1 |   104 |    7  (15)| 00:00:01 |

|*  2 |   TABLE ACCESS FULL| TMP2 |    1 |    52 |    3   (0)| 00:00:01 |

|*  3 |   TABLE ACCESS FULL| TMP1 |    3 |   156 |    3   (0)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

1 - access("TMP1"."A"="TMP2"."A")
    2 - filter("TMP2"."E"<=3 AND "TMP2"."D">1 AND "B"<30)
    3 - filter("TMP1"."B"<50 AND "TMP1"."E">=2 AND "C">=1)

Note

-----
    - dynamic sampling used for this statement (level=2)

Statistics

----------------------------------------------------------
       9  recursive calls
       0  db block gets
      15  consistent gets
       0  physical reads
       0  redo size
     981  bytes sent via SQL*Net to client
     524  bytes received via SQL*Net from client
       2  SQL*Net roundtrips to/from client
       0  sorts (memory)
       0  sorts (disk)
       1  rows processed

SQL>

postgres=# select *

postgres-# from (

postgres(# select * from tmp1 where c >= 1

postgres(# ) t1 left join (

postgres(# select * from tmp2 where b < 30

postgres(# ) t2 on t1.a = t2.a

postgres-# and t2.d > 1 and t1.e >= 2

postgres-# where t1.b < 50 and t2.e <= 3

postgres-# ;
  a | b  | c | e | a | b  | d | e

---+----+---+---+---+----+---+---
  2 | 20 | 2 | 2 | 2 | 20 | 2 | 2

(1 row)

postgres=# explain analyze select *

postgres-# from (

postgres(# select * from tmp1 where c >= 1

postgres(# ) t1 left join (

postgres(# select * from tmp2 where b < 30

postgres(# ) t2 on t1.a = t2.a

postgres-# and t2.d > 1 and t1.e >= 2

postgres-# where t1.b < 50 and t2.e <= 3

postgres-# ;
                                                  QUERY PLAN                                                 

-------------------------------------------------------------------------------------------------------------
  Hash Join  (cost=38.68..78.43 rows=18 width=32) (actual time=0.033..0.041 rows=1 loops=1)
    Hash Cond: ("outer".a = "inner".a)
    ->  Seq Scan on tmp1  (cost=0.00..38.53 rows=60 width=16) (actual time=0.007..0.011 rows=3 loops=1)
          Filter: ((c >= 1) AND (e >= 2) AND (b < 50))
    ->  Hash  (cost=38.53..38.53 rows=60 width=16) (actual time=0.008..0.008 rows=1 loops=1)
          ->  Seq Scan on tmp2  (cost=0.00..38.53 rows=60 width=16) (actual time=0.003..0.005 rows=1 loops=1)
                Filter: ((b < 30) AND (d > 1) AND (e <= 3))
  Total runtime: 0.070 ms

(8 rows)

postgres=#

SQL执行计划的分析:

1) 全表扫描左表TMP2,同时根据TMP2表子查询条件"B"<30和where过滤条件"TMP2"."E"<=3及ON子句过滤条件"TMP2"."D">1联合过滤,即filter("TMP2"."E"<=3 AND "TMP2"."D">1 AND "B"<30),计算结果临时表记为tmp1;

2) 全表扫描右表TMP1,同时根据TMP1表子查询条件"C">=1和where子句过滤条件"TMP1"."B"<50及ON子句"TMP1"."E">=2联合过滤,即filter("TMP1"."B"<50 AND "TMP1"."E">=2 AND "C">=1),计算结果临时表记为tmp2;

3) 临时表tmp1和tmp2通过access("TMP1"."A"="TMP2"."A")连接条件进行Hash Join连接操作(此处left join写法已经被转换为内链接),返回匹配结果临时表记为tmp3;

4) 返回结果集。

两个左连接SQL执行计划解析(Oracle和PGSQL对比):的更多相关文章

  1. 一个RDBMS左连接SQL执行计划解析

    1.测试数据如下: SQL> select * from t1;  a | b  | c ---+----+---  1 | 10 | 1  2 | 20 | 2  3 | 30 | 3  4 ...

  2. Oracle sql执行计划解析

    Oracle sql执行计划解析 https://blog.csdn.net/xybelieve1990/article/details/50562963 Oracle优化器 Oracle的优化器共有 ...

  3. sql执行计划解析案例(二)

    sql执行计划解析案例(二)   今天是2013-10-09,本来以前自己在专注oracle sga中buffer cache 以及shared pool知识点的研究.但是在研究cache buffe ...

  4. 表连接sql执行计划学习

    循环嵌套连接(Nested Loop Join) 合并连接(Merge Join) 哈西匹配(Hash Join) 文章:浅谈SQL Server中的三种物理连接操作 循环嵌套,如果内循环列上有索引, ...

  5. oracle sql 执行计划分析

    转自http://itindex.net/detail/45962-oracle-sql-%E8%AE%A1%E5%88%92 一.首先创建表 SQL> show user USER is &q ...

  6. 来自灵魂的拷问——知道什么是SQL执行计划吗?

    面试官说:工作这么久了,应该知道sql执行计划吧,讲讲Sql的执行计划吧! 看了看面试官手臂上纹的大花臂和一串看不懂的韩文,吞了吞口水,暗示自己镇定点,整理了一下思绪缓缓的对面试官说:我不会 面试官: ...

  7. Atitit sql执行计划

    Atitit sql执行计划 1.1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的 Oracle中的执行计划 ...

  8. [转载]循规蹈矩:快速读懂SQL执行计划的套路与工具

    作者介绍 梁敬彬,福富研究院副理事长.公司唯一四星级内训师,国内一线知名数据库专家,在数据库优化和培训领域有着丰富的经验.多次应邀担任国内外数据库大会的演讲嘉宾,在业界有着广泛的影响力.著有多本畅销书 ...

  9. EXPLAIN 查看 SQL 执行计划

    EXPLAIN 查看 SQL 执行计划.分析索引的效率: id:id 列数字越大越先执行: 如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它来进行查询. ...

随机推荐

  1. vue安装scss,并且全局引入

    在写vue的css样式时,觉得需要css预处理器让自己的css更加简洁.适应性更强.可读性更佳,更易于代码的维护,于是在vue-cli脚手架采用scss.写过的人都知道,每写一个.vue文件都要在st ...

  2. ng-app&data-ng-app

    来源stackoverflow 区别:在验证html5时,ng-app会抛出一个错误,而对带data-前缀的特性不会抛出.其它方面这两个属性一样.

  3. 分布式系列六: WebService简介

    WebSerice盛行的时代已经过去, 这里只是简单介绍下其基本概念, 并用JDK自带的API实现一个简单的服务. WebSerice的概念 WebService是一种跨平台和跨语言的远程调用(RPC ...

  4. Node.js使用jszip实现打包zip压缩包

    一.前言 最近有这样的一个需求,需要把两个同名的.mtl文件和.obj文件打包成一个同名的.zip压缩包.刚开始文件不多的时候,只有几个,或者十几个,甚至二三十个的时候,还能勉强接受手动修改,但是随着 ...

  5. VUE中总的逻辑关系和移动端mint-ui的应用接触

    1.mint-ui官网:https://mint-ui.github.io/#!/zh-cn 可以点击开始使用,里边有详细的讲解.安装mint-ui: 官网是: 但是应用没有装成功,不知为何,可能是我 ...

  6. 初识C语言(一)

    C语言的结构体 一个C程序就是由多个头文件和函数组成 #include<stdio.h> /* 包含头文件*/ int main() { printf('"hello world ...

  7. 进入django

    web应用,c/s,b/s架构 c/s: 客户端 服务端 b/s: 浏览器 服务器 HTTP协议: 超文本传输协议 四大特性: 1.基于TCP/IP作用在应用层之上的协议 2.基于请求响应 3.无状态 ...

  8. win7下安装linux(centos6.5)双系统详细小白教程

    在正式介绍linux安装教程之前,先声明一下本人也是刚开始接触linux,所以教程只以成功安装linux为目标,里面的具体步骤我都是参考网上的教程自己操作实现的,至于为什么要这么做就不多做解释,大家想 ...

  9. SpringBoot 自定义监听器(Listener)

    1. 使用场景:在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载.初始化缓存.特定任务的注册.开启线程或程序来干某些事情等等. 2. 使用步骤: A. 监听类实现Appli ...

  10. git本地项目上传至码云gitee

    如果你的本机是安装成功第一次使用,先配置一下一些基本的信息 $ git config--global user.name "Your Name" $ git config --gl ...