PostgreSQL的查询语句的连接方式与查询计划比较--多表连接(二)

续: PostgreSQL的查询语句的连接方式与查询计划比较--多表连接(一)

--step5:对比step3,增加连接条件(连接条件变多但多余的条件可推导出相等故可合并)

test=# explain select * from A, B, C, D where A.c1=B.c1 and B.c1=C.c1 AND C.c1=D.c1 and A.c1=C.c1 and B.c1=D.c1;

                                  QUERY PLAN

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

 Merge Join  (cost=541.37..27731.14 rows=1770590 width=48)

   Merge Cond: (a.c1 = c.c1)

   ->  Merge Join  (cost=270.68..562.65 rows=18818 width=24)

         Merge Cond: (a.c1 = b.c1)

         ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

               Sort Key: a.c1

               ->  Seq Scan on a  (cost=0.00..29.40 rows=1940 width=12)

         ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

               Sort Key: b.c1

               ->  Seq Scan on b  (cost=0.00..29.40 rows=1940 width=12)

   ->  Materialize  (cost=270.68..609.70 rows=18818 width=24)

         ->  Merge Join  (cost=270.68..562.65 rows=18818 width=24)

               Merge Cond: (c.c1 = d.c1)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: c.c1

                     ->  Seq Scan on c  (cost=0.00..29.40 rows=1940 width=12)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: d.c1

                     ->  Seq Scan on d  (cost=0.00..29.40 rows=1940 width=12)

(19 rows)

分析:

1)   连接条件与setp3比,查询计划没有特殊变化

2)   连接条件可以推导出:A.c1B.c1C.c1D.c1是相等的,所以和step3的查询计划没有什么变化

 

--step6:对比step3,增加连接条件

test=# explain select * from A, B, C, D where A.c1=B.c1 and B.c1=C.c1 AND C.c1=D.c1 and A.c2=C.c2 and B.c2=D.c2;

                                        QUERY PLAN

 

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

----------

 Merge Join  (cost=547.27..569.10 rows=44 width=48)

   Merge Cond: ((a.c1 = b.c1) AND (d.c2 = b.c2))

   ->  Sort  (cost=411.93..414.21 rows=912 width=36)

         Sort Key: a.c1, d.c2

         ->  Hash Join  (cost=301.90..367.09 rows=912 width=36)

               Hash Cond: (d.c1 = a.c1)

               ->  Seq Scan on d  (cost=0.00..29.40 rows=1940 width=12)

               ->  Hash  (cost=300.72..300.72 rows=94 width=24)

                     ->  Merge Join  (cost=270.68..300.72 rows=94 width=24)

                           Merge Cond: ((a.c1 = c.c1) AND (a.c2 = c.c2))

                           ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                                 Sort Key: a.c1, a.c2

                                 ->  Seq Scan on a  (cost=0.00..29.40 rows=1940

width=12)

                           ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                                 Sort Key: c.c1, c.c2

                                 ->  Seq Scan on c  (cost=0.00..29.40 rows=1940

width=12)

   ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

         Sort Key: b.c1, b.c2

         ->  Seq Scan on b  (cost=0.00..29.40 rows=1940 width=12)

(19 rows)

分析:

1)   连接条件与setp3比,连接次序依旧是:AB->ABC->ABCD

 

--step6:对比step3step4,增加新的连接条件,减少可推导出4个关系相等的连接条件

test=# explain select * from A, B, C, D where A.c1=B.c1 AND C.c1=D.c1 and A.c2=C.c2 and B.c2=D.c2;

                                  QUERY PLAN

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

 Merge Join  (cost=3797.43..4168.23 rows=8853 width=48)

   Merge Cond: ((a.c2 = c.c2) AND (b.c2 = d.c2))

   ->  Sort  (cost=1898.72..1945.76 rows=18818 width=24)

         Sort Key: a.c2, b.c2

         ->  Merge Join  (cost=270.68..562.65 rows=18818 width=24)

               Merge Cond: (a.c1 = b.c1)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: a.c1

                     ->  Seq Scan on a  (cost=0.00..29.40 rows=1940 width=12)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: b.c1

                     ->  Seq Scan on b  (cost=0.00..29.40 rows=1940 width=12)

   ->  Sort  (cost=1898.72..1945.76 rows=18818 width=24)

         Sort Key: c.c2, d.c2

         ->  Merge Join  (cost=270.68..562.65 rows=18818 width=24)

               Merge Cond: (c.c1 = d.c1)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: c.c1

                     ->  Seq Scan on c  (cost=0.00..29.40 rows=1940 width=12)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: d.c1

                     ->  Seq Scan on d  (cost=0.00..29.40 rows=1940 width=12)

(22 rows)

分析:

1)   step3比,新增ACBD之间的连接条件(A.c2=C.c2 and B.c2=D.c2)

2)   step3比,减少可推导出AC之间相等的连接条件(B.c1=C.c1)

3)   连接条件与setp3比,连接次序是紧密树方式:ABCD->ABCD

4)   连接算法采用的是归并连接(因为每2个关系,都依据连接条件排序)

 

--step7:对比step3,减少连接条件

test=# explain select * from A, B, C, D where A.c1=B.c1 and B.c1=C.c1;

                                  QUERY PLAN

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

 Nested Loop  (cost=324.33..4433560.06 rows=354117900 width=48)

   ->  Hash Join  (cost=324.33..7052.06 rows=182535 width=36)

         Hash Cond: (a.c1 = c.c1)

         ->  Merge Join  (cost=270.68..562.65 rows=18818 width=24)

               Merge Cond: (a.c1 = b.c1)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: a.c1

                     ->  Seq Scan on a  (cost=0.00..29.40 rows=1940 width=12)

               ->  Sort  (cost=135.34..140.19 rows=1940 width=12)

                     Sort Key: b.c1

                     ->  Seq Scan on b  (cost=0.00..29.40 rows=1940 width=12)

         ->  Hash  (cost=29.40..29.40 rows=1940 width=12)

               ->  Seq Scan on c  (cost=0.00..29.40 rows=1940 width=12)

   ->  Materialize  (cost=0.00..39.10 rows=1940 width=12)

         ->  Seq Scan on d  (cost=0.00..29.40 rows=1940 width=12)

(15 rows)

分析:

1)   setp3比,连接次序依旧是:AB->ABC->ABCD

2)   在关系D上,不存在连接条件,所以对于关系DABC这个新关系,做得是笛卡尔积

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL是以加州大学伯克利分校计算机系开发的POSTGRES,现在已经更名为PostgreSQL. PostgreSQL支持大部分SQL标准并且提供了许多其它现代特性:复杂查询、外键、触发器、视图、事务完整性等。 PostgreSQL 是一个免费的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发行。它提供了相对其他开放源代码数据库系统(比如 MySQL 和 Firebird),和专有系统(比如 Oracle、Sybase、IBM 的 DB2 和 Microsoft SQL Server)之外的另一种选择。 事实上, PostgreSQL 的特性覆盖了 SQL-2/SQL-92 和 SQL-3/SQL-99,首先,它包括了可以说是目前世界上最丰富的数据类型的支持,其中有些数据类型可以说连商业数据库都不具备, 比如 IP 类型和几何类型等;其次,PostgreSQL 是全功能的自由软件数据库,很长时间以来,PostgreSQL 是唯一支持事务、子查询、多版本并行控制系统(MVCC)、数据完整性检查等特性的唯一的一种自由软件的数据库管理系统。 Inprise 的 InterBase 以及SAP等厂商将其原先专有软件开放为自由软件之后才打破了这个唯一。最后,PostgreSQL拥有一支非常活跃的开发队伍,而且在许多黑客的努力下,PostgreSQL 的质量日益提高。从技术角度来讲,PostgreSQL 采用的是比较经典的C/S(client/server)结构,也就是一个客户端对应一个服务器端守护进程的模式,这个守护进程分析客户端来的查询请求,生成规划树,进行数据检索并最终把结果格式化输出后返回给客户端。为了便于客户端的程序的编写,由数据库服务器提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如ODBC,JDBC,Python,Perl,Tcl,C/C++,ESQL等, 同时也要指出的是,PostgreSQL 对接口的支持也是非常丰富的,几乎支持所有类型的数据库客户端接口。这一点也可以说是 PostgreSQL 一大优点。 本课程作为PostgreSQL数据库管理之三,主要讲解以下内容:1.     PostgreSQL约束讲解和剖析2.     PostgreSQL数据类型3.     PostgreSQL的结构管理4.     PostgreSQL条件表达式和操作5.     PostgreSQL使用小技巧

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值