PostgreSQL新特性分析

這篇文章主要介紹“PostgreSQL新特性分析”,在日常操作中,相信很多人在PostgreSQL新特性分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL新特性分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)公司秉承實現(xiàn)全網(wǎng)價值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè)微信小程序定制開發(fā),網(wǎng)頁設(shè)計制作,成都做手機網(wǎng)站網(wǎng)絡(luò)營銷推廣幫助傳統(tǒng)企業(yè)實現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對客戶都以感恩的心態(tài)奉獻自己的專業(yè)和所長。

在PG 8.4 ~ PG 11,PG會把WITH中的查詢視為”optimization fence”(優(yōu)化圍欄,與WITH外的查詢隔離,獨立優(yōu)化),也就意味著謂詞下推等優(yōu)化手段無法應(yīng)用到WITH子句中,考慮到CTE在大多數(shù)情況下是為了增強可讀性而存在,因此在PG 12中,滿足以下三個條件的,優(yōu)化器將不會對CTE”視而不見”而是執(zhí)行”積極的”優(yōu)化.
A.遞歸查詢
B.沒有任何副作用(side effect)
C.僅在查詢的后續(xù)部分引用一次

謂詞下推
測試腳本:

drop table  if exists t_w1;
drop table  if exists t_w2;
drop table  if exists t_w3;
create table t_w1(id int ,c1 varchar(20));
create table t_w2(id int ,c1 varchar(20));
create table t_w3(id int ,c1 varchar(20));
insert into t_w1 select x,x||'' from generate_series(1,10000) as x;
insert into t_w2 select x/2,(x/2)||'' from generate_series(1,10000) as x;
insert into t_w3 select x,x||'' from generate_series(1,10000) as x;

查詢語句:

WITH t1 AS ( SELECT * FROM t_w1 WHERE t_w1.id % 4 = 0 ) 
SELECT * FROM t1 
  JOIN t_w2 as t2 
  ON t2.id = t1.id
     AND t1.id < 100;

在PG 11中,其執(zhí)行計劃如下:

version                                                 
--------------------------------------------------------------------------------------------
 PostgreSQL 11.2 on x86_XX-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), XX-bit
(1 row)
testdb=# explain analyze WITH t1 AS ( SELECT * FROM t_w1 WHERE t_w1.id % 4 = 0 ) 
testdb-# SELECT * FROM t1 
testdb-#   JOIN t_w2 as t2 
testdb-#   ON t2.id = t1.id 
testdb-#      AND t1.id < 100;
                                                   QUERY PLAN                                                    
--------------------------------------------------------------------------------------------
 Hash Join  (cost=205.34..396.18 rows=34 width=70) (actual time=8.576..11.187 rows=48 loops=1)
   Hash Cond: (t2.id = t1.id)
   CTE t1
     ->  Seq Scan on t_w1  (cost=0.00..204.00 rows=50 width=8) (actual time=0.029..6.074 rows=2500 loops=1)
           Filter: ((id % 4) = 0)
           Rows Removed by Filter: 7500
   ->  Seq Scan on t_w2 t2  (cost=0.00..153.00 rows=10000 width=8) (actual time=0.030..1.166 rows=10000 loops=1)
   ->  Hash  (cost=1.12..1.12 rows=17 width=62) (actual time=8.536..8.536 rows=24 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 9kB
         ->  CTE Scan on t1  (cost=0.00..1.12 rows=17 width=62) (actual time=0.033..8.521 rows=24 loops=1)
               Filter: (id < 100)
               Rows Removed by Filter: 2476
 Planning Time: 1.913 ms
 Execution Time: 11.357 ms
(14 rows)

在PG 12中,其執(zhí)行計劃如下:

testdb=# select version();
                                                  version                                                   
--------------------------------------------------------------------------------------------
 PostgreSQL 12beta1 on x86_XX-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), XX-bit
(1 row)
testdb=# explain analyze WITH t1 AS ( SELECT * FROM t_w1 WHERE t_w1.id % 4 = 0 ) 
testdb-# SELECT * FROM t1 
testdb-#   JOIN t_w2 as t2 
testdb-#   ON t2.id = t1.id 
testdb-#      AND t1.id < 100;
                                                   QUERY PLAN                                                    
--------------------------------------------------------------------------------------------
 Hash Join  (cost=229.01..419.52 rows=1 width=16) (actual time=6.974..17.156 rows=48 loops=1)
   Hash Cond: (t2.id = t_w1.id)
   ->  Seq Scan on t_w2 t2  (cost=0.00..153.00 rows=10000 width=8) (actual time=0.076..5.205 rows=10000 loops=1)
   ->  Hash  (cost=229.00..229.00 rows=1 width=8) (actual time=6.882..6.882 rows=24 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 9kB
         ->  Seq Scan on t_w1  (cost=0.00..229.00 rows=1 width=8) (actual time=0.077..6.842 rows=24 loops=1)
               Filter: ((id < 100) AND ((id % 4) = 0))
               Rows Removed by Filter: 9976
 Planning Time: 1.677 ms
 Execution Time: 17.244 ms
(10 rows)

可以看到,在PG 11中,謂詞(id < 100)不會下推CTE中,但在PG 12中,優(yōu)化器則把謂詞下推到CTE中(Filter: ((id < 100) AND ((id % 4) = 0))).

New Option
如果希望12的優(yōu)化器行為與先前的一樣,則加入Option : MATERIALIZED.

testdb=# explain analyze WITH t1 AS MATERIALIZED( SELECT * FROM t_w1 WHERE t_w1.id % 4 = 0 ) 
SELECT * FROM t1 
  JOIN t_w2 as t2 
  ON t2.id = t1.id 
     AND t1.id < 100;
                                                   QUERY PLAN                                                    
-------------------------------------------------------------------------------------------
 Hash Join  (cost=205.34..396.18 rows=34 width=70) (actual time=30.705..48.549 rows=48 loops=1)
   Hash Cond: (t2.id = t1.id)
   CTE t1
     ->  Seq Scan on t_w1  (cost=0.00..204.00 rows=50 width=8) (actual time=0.152..21.274 rows=2500 loops=1)
           Filter: ((id % 4) = 0)
           Rows Removed by Filter: 7500
   ->  Seq Scan on t_w2 t2  (cost=0.00..153.00 rows=10000 width=8) (actual time=0.154..8.582 rows=10000 loops=1)
   ->  Hash  (cost=1.12..1.12 rows=17 width=62) (actual time=30.502..30.502 rows=24 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 9kB
         ->  CTE Scan on t1  (cost=0.00..1.12 rows=17 width=62) (actual time=0.168..30.445 rows=24 loops=1)
               Filter: (id < 100)
               Rows Removed by Filter: 2476
 Planning Time: 7.673 ms
 Execution Time: 49.284 ms
(14 rows)

如果希望優(yōu)化器把盡可能的把CTE視為內(nèi)聯(lián)查詢進行優(yōu)化,則指定NOT MATERIALIZED Option:
下面的查詢,CTE被引用多次,優(yōu)化器默認會進行MATERIALIZED,通過指定NOT MATERIALIZED則強制為內(nèi)聯(lián)查詢.

testdb=# explain analyze WITH t1 AS ( SELECT * FROM t_w1 WHERE t_w1.id % 4 = 0 ) 
testdb-# SELECT * FROM t1 
testdb-#   JOIN t_w2 as t2 
testdb-#   ON t2.id = t1.id
testdb-# UNION ALL
testdb-# select t1.*,NULL,NULL from t1 where t1.id % 3 = 0;
                                                      QUERY PLAN                                                       
-----------------------------------------------------------------------------------------------------------------------
 Append  (cost=205.62..399.89 rows=101 width=70) (actual time=11.663..27.725 rows=3332 loops=1)
   CTE t1
     ->  Seq Scan on t_w1  (cost=0.00..204.00 rows=50 width=8) (actual time=0.032..7.300 rows=2500 loops=1)
           Filter: ((id % 4) = 0)
           Rows Removed by Filter: 7500
   ->  Hash Join  (cost=1.62..193.12 rows=100 width=70) (actual time=11.662..24.094 rows=2499 loops=1)
         Hash Cond: (t2.id = t1.id)
         ->  Seq Scan on t_w2 t2  (cost=0.00..153.00 rows=10000 width=8) (actual time=0.033..4.412 rows=10000 loops=1)
         ->  Hash  (cost=1.00..1.00 rows=50 width=62) (actual time=11.611..11.612 rows=2500 loops=1)
               Buckets: 4096 (originally 1024)  Batches: 1 (originally 1)  Memory Usage: 132kB
               ->  CTE Scan on t1  (cost=0.00..1.00 rows=50 width=62) (actual time=0.035..9.916 rows=2500 loops=1)
   ->  CTE Scan on t1 t1_1  (cost=0.00..1.25 rows=1 width=98) (actual time=0.008..2.824 rows=833 loops=1)
         Filter: ((id % 3) = 0)
         Rows Removed by Filter: 1667
 Planning Time: 2.358 ms
 Execution Time: 28.746 ms
(16 rows)

使用NOT MATERIALIZED選項

testdb=# explain analyze WITH t1 AS NOT MATERIALIZED( SELECT * FROM t_w1 WHERE t_w1.id % 4 = 0 ) 
SELECT * FROM t1 
  JOIN t_w2 as t2 
  ON t2.id = t1.id
UNION ALL
select t1.*,NULL,NULL from t1 where t1.id % 3 = 0;
                                                      QUERY PLAN                                                       
-------------------------------------------------------------------------------------------
 Append  (cost=204.62..650.39 rows=51 width=17) (actual time=27.894..57.453 rows=3332 loops=1)
   ->  Hash Join  (cost=204.62..395.62 rows=50 width=16) (actual time=27.892..48.911 rows=2499 loops=1)
         Hash Cond: (t2.id = t_w1.id)
         ->  Seq Scan on t_w2 t2  (cost=0.00..153.00 rows=10000 width=8) (actual time=0.149..7.606 rows=10000 loops=1)
         ->  Hash  (cost=204.00..204.00 rows=50 width=8) (actual time=27.699..27.699 rows=2500 loops=1)
               Buckets: 4096 (originally 1024)  Batches: 1 (originally 1)  Memory Usage: 132kB
               ->  Seq Scan on t_w1  (cost=0.00..204.00 rows=50 width=8) (actual time=0.151..22.446 rows=2500 loops=1)
                     Filter: ((id % 4) = 0)
                     Rows Removed by Filter: 7500
   ->  Seq Scan on t_w1 t_w1_1  (cost=0.00..254.00 rows=1 width=44) (actual time=0.038..7.400 rows=833 loops=1)
         Filter: (((id % 4) = 0) AND ((id % 3) = 0))
         Rows Removed by Filter: 9167
 Planning Time: 12.357 ms
 Execution Time: 58.490 ms
(14 rows)

到此,關(guān)于“PostgreSQL新特性分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

文章名稱:PostgreSQL新特性分析
URL鏈接:http://bm7419.com/article34/ijpgse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗電子商務(wù)、企業(yè)網(wǎng)站制作、網(wǎng)站策劃網(wǎng)站維護、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站