PostgreSQL difference between filter and join filter

1. filter: 普通的过滤条件,where ...; having ...
   join filter: 多表联结的时候,表与表之间联结时候的过滤条件

   区分内联结和外联结来说明区别。


2. 建表,插入测试数据

postgres=# create table tb13(id integer,info character varying);
CREATE TABLE
postgres=# create table tb14(id integer,info character varying);
CREATE TABLE
postgres=# 
postgres=# insert into tb13 values(1,'tb13');
INSERT 0 1
postgres=# insert into tb14 values(1,'tb14');
INSERT 0 1

3. outer join:filter和join filter有区别。
①、使用join filter
postgres=# select * from tb13 left join tb14 on tb13.id=tb14.id and tb13.info='no';
 id | info | id | info 
----+------+----+------
  1 | tb13 |    | 
(1 row)
可以看到过滤条件tb13.info='no'对表tb13来说没有起作用。
查看执行计划:
postgres=# explain analyze select * from tb13 left join tb14 on tb13.id=tb14.id and tb13.info='no';
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Merge Left Join  (cost=170.85..309.37 rows=1230 width=72) (actual time=0.027..0.027 rows=1 loops=1)
   Merge Cond: (tb13.id = tb14.id)
   <span style="color:#ff0000;">Join Filter: ((tb13.info)::text = 'no'::text)</span>
   Rows Removed by Join Filter: 1
   ->  Sort  (cost=85.43..88.50 rows=1230 width=36) (actual time=0.014..0.014 rows=1 loops=1)
         Sort Key: tb13.id
         Sort Method: quicksort  Memory: 25kB
         ->  Seq Scan on tb13  (cost=0.00..22.30 rows=1230 width=36) (actual time=0.007..0.008 rows=1 loops=1)
   ->  Sort  (cost=85.43..88.50 rows=1230 width=36) (actual time=0.004..0.004 rows=1 loops=1)
         Sort Key: tb14.id
         Sort Method: quicksort  Memory: 25kB
         ->  Seq Scan on tb14  (cost=0.00..22.30 rows=1230 width=36) (actual time=0.002..0.002 rows=1 loops=1)
 Total runtime: 0.067 ms
(13 rows)
②、使用filter
postgres=# select * from tb13 left join tb14 on tb13.id=tb14.id where tb13.info='no';
 id | info | id | info 
----+------+----+------
(0 rows)
可以看到过滤条件tb13.info='no'已经起作用。
查看执行计划:
postgres=# explain analyze select * from tb13 left join tb14 on tb13.id=tb14.id where tb13.info='no';
                                                 QUERY PLAN                                                 
------------------------------------------------------------------------------------------------------------
 Hash Right Join  (cost=25.45..52.73 rows=37 width=72) (actual time=5.707..5.707 rows=0 loops=1)
   Hash Cond: (tb14.id = tb13.id)
   ->  Seq Scan on tb14  (cost=0.00..22.30 rows=1230 width=36) (never executed)
   ->  Hash  (cost=25.38..25.38 rows=6 width=36) (actual time=0.017..0.017 rows=0 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 0kB
         ->  Seq Scan on tb13  (cost=0.00..25.38 rows=6 width=36) (actual time=0.017..0.017 rows=0 loops=1)
               <span style="color:#ff0000;">Filter: ((info)::text = 'no'::text)</span>
               Rows Removed by Filter: 1
 Total runtime: 34.031 ms
(9 rows)
4. inner join: filter和join filter没有区别。
①、使用 join filter

postgres=# select * from tb13 inner join tb14 on tb13.id=tb14.id and tb13.info='no';
 id | info | id | info 
----+------+----+------
(0 rows)
查看执行计划:
postgres=# explain analyze select * from tb13 inner join tb14 on tb13.id=tb14.id and tb13.info='no';
                                                 QUERY PLAN                                                 
------------------------------------------------------------------------------------------------------------
 Hash Join  (cost=25.45..52.73 rows=37 width=72) (actual time=0.034..0.034 rows=0 loops=1)
   Hash Cond: (tb14.id = tb13.id)
   ->  Seq Scan on tb14  (cost=0.00..22.30 rows=1230 width=36) (actual time=0.006..0.006 rows=1 loops=1)
   ->  Hash  (cost=25.38..25.38 rows=6 width=36) (actual time=0.022..0.022 rows=0 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 0kB
         ->  Seq Scan on tb13  (cost=0.00..25.38 rows=6 width=36) (actual time=0.022..0.022 rows=0 loops=1)
               <span style="color:#ff0000;">Filter: ((info)::text = 'no'::text)</span>
               Rows Removed by Filter: 1
 Total runtime: 0.062 ms
(9 rows)
②、使用 filter
postgres=# select * from tb13 inner join tb14 on tb13.id=tb14.id where tb13.info='no';
 id | info | id | info 
----+------+----+------
(0 rows)
查看执行计划:
postgres=# explain analyze select * from tb13 inner join tb14 on tb13.id=tb14.id where tb13.info='no';
                                                 QUERY PLAN                                                 
------------------------------------------------------------------------------------------------------------
 Hash Join  (cost=25.45..52.73 rows=37 width=72) (actual time=0.015..0.015 rows=0 loops=1)
   Hash Cond: (tb14.id = tb13.id)
   ->  Seq Scan on tb14  (cost=0.00..22.30 rows=1230 width=36) (actual time=0.006..0.006 rows=1 loops=1)
   ->  Hash  (cost=25.38..25.38 rows=6 width=36) (actual time=0.004..0.004 rows=0 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 0kB
         ->  Seq Scan on tb13  (cost=0.00..25.38 rows=6 width=36) (actual time=0.004..0.004 rows=0 loops=1)
               <span style="color:#ff0000;">Filter: ((info)::text = 'no'::text)</span>
               Rows Removed by Filter: 1
 Total runtime: 0.036 ms
(9 rows)

可以看到,过滤条件都起作用,在执行计划里面也可以看到在内联结中都是当作filter来操作的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值