HTAP資料庫PostgreSQL場景與效能測試之26-(OLTP)NOTIN、NOTEXISTS查詢

德哥發表於2017-11-14

標籤

PostgreSQL , HTAP , OLTP , OLAP , 場景與效能測試


背景

PostgreSQL是一個歷史悠久的資料庫,歷史可以追溯到1973年,最早由2014計算機圖靈獎得主,關聯式資料庫的鼻祖Michael_Stonebraker 操刀設計,PostgreSQL具備與Oracle類似的功能、效能、架構以及穩定性。

pic

PostgreSQL社群的貢獻者眾多,來自全球各個行業,歷經數年,PostgreSQL 每年釋出一個大版本,以持久的生命力和穩定性著稱。

2017年10月,PostgreSQL 推出10 版本,攜帶諸多驚天特性,目標是勝任OLAP和OLTP的HTAP混合場景的需求:

《最受開發者歡迎的HTAP資料庫PostgreSQL 10特性》

1、多核並行增強

2、fdw 聚合下推

3、邏輯訂閱

4、分割槽

5、金融級多副本

6、json、jsonb全文檢索

7、還有外掛化形式存在的特性,如 向量計算、JIT、SQL圖計算、SQL流計算、分散式平行計算、時序處理、基因測序、化學分析、影像分析 等。

pic

在各種應用場景中都可以看到PostgreSQL的應用:

pic

PostgreSQL近年來的發展非常迅猛,從知名資料庫評測網站dbranking的資料庫評分趨勢,可以看到PostgreSQL向上發展的趨勢:

pic

從每年PostgreSQL中國召開的社群會議,也能看到同樣的趨勢,參與的公司越來越多,分享的公司越來越多,分享的主題越來越豐富,橫跨了 傳統企業、網際網路、醫療、金融、國企、物流、電商、社交、車聯網、共享XX、雲、遊戲、公共交通、航空、鐵路、軍工、培訓、諮詢服務等 行業。

接下來的一系列文章,將給大家介紹PostgreSQL的各種應用場景以及對應的效能指標。

環境

環境部署方法參考:

《PostgreSQL 10 + PostGIS + Sharding(pg_pathman) + MySQL(fdw外部表) on ECS 部署指南(適合新使用者)》

阿里雲 ECS:56核,224G,1.5TB*2 SSD雲盤

作業系統:CentOS 7.4 x64

資料庫版本:PostgreSQL 10

PS:ECS的CPU和IO效能相比物理機會打一定的折扣,可以按下降1倍效能來估算。跑物理主機可以按這裡測試的效能乘以2來估算。

場景 – NOT IN、NOT EXISTS 查詢 (OLTP)

1、背景

not in 查詢,多用在排除多個輸入值場景。

實際上PostgreSQL支援很多種排除多個輸入值的語法。

1、not in (...)

2、not in (table or subquery or srf)

3、<> all (array)

4、not exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

5、<>? and <>? and <>? and .....

6、left join others b on (a.?=b.?) where b.* is null

7、select ? from a except select ? from b,適用於輸出欄位與條件欄位相同的情形。

他們的執行計劃分別如下,(1億記錄,排除多個輸入值。):

表越大、或Filter的值越多,使用 left join, not exist, except 的效果越好。

postgres=# explain select * from a where id not in (1,2,3,4,5);  
                          QUERY PLAN  
--------------------------------------------------------------  
 Seq Scan on a  (cost=0.00..255958.10 rows=10000001 width=45)  
   Filter: (id <> ALL (`{1,2,3,4,5}`::integer[]))  
(2 rows)  
  
postgres=# explain select * from a where id <> all (array[1,2,3,4,5]);  
                          QUERY PLAN  
--------------------------------------------------------------  
 Seq Scan on a  (cost=0.00..255958.10 rows=10000001 width=45)  
   Filter: (id <> ALL (`{1,2,3,4,5}`::integer[]))  
(2 rows)  
  
postgres=# explain select * from a where id <> all (array(select generate_series(1,10)));  
                         QUERY PLAN  
-------------------------------------------------------------  
 Seq Scan on a  (cost=5.02..318463.15 rows=9999996 width=45)  
   Filter: (id <> ALL ($0))  
   InitPlan 1 (returns $0)  
     ->  ProjectSet  (cost=0.00..5.02 rows=1000 width=4)  
           ->  Result  (cost=0.00..0.01 rows=1 width=0)  
(5 rows)  
  
postgres=# explain select * from a where id <> all (array(select id from (values (1),(2),(3),(4),(5)) t (id)));  
                             QUERY PLAN  
---------------------------------------------------------------------  
 Seq Scan on a  (cost=0.06..318458.20 rows=9999996 width=45)  
   Filter: (id <> ALL ($0))  
   InitPlan 1 (returns $0)  
     ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
(4 rows)  
  
postgres=# explain select * from a where id not in (select id from (values (1),(2),(3),(4),(5)) t (id));  
                             QUERY PLAN  
---------------------------------------------------------------------  
 Seq Scan on a  (cost=0.07..218458.15 rows=5000003 width=45)  
   Filter: (NOT (hashed SubPlan 1))  
   SubPlan 1  
     ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
(4 rows)  
  
postgres=# explain select * from a where not exists (select 1 from (values (1),(2),(3),(4),(5)) t (id) where t.id=a.id);  
                                    QUERY PLAN  
-----------------------------------------------------------------------------------  
 Merge Anti Join  (cost=0.56..301364.14 rows=10000001 width=45)  
   Merge Cond: (a.id = "*VALUES*".column1)  
   ->  Index Scan using a_pkey on a  (cost=0.43..276363.92 rows=10000006 width=45)  
   ->  Sort  (cost=0.12..0.13 rows=5 width=4)  
         Sort Key: "*VALUES*".column1  
         ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
(6 rows)  
  
postgres=# explain select * from a where id<>1 and id<>2 and id<>3 and id<>4 and id<>5;  
                                  QUERY PLAN  
-------------------------------------------------------------------------------  
 Seq Scan on a  (cost=0.00..318458.14 rows=10000001 width=45)  
   Filter: ((id <> 1) AND (id <> 2) AND (id <> 3) AND (id <> 4) AND (id <> 5))  
(2 rows)  
  
postgres=# explain with t1 as (select id from (values (1),(2),(3),(4),(5)) as t(id))  
                   select a.* from a left join t1 b on (a.id=b.id) where b.* is null;  
                             QUERY PLAN  
---------------------------------------------------------------------  
 Hash Left Join  (cost=0.23..230958.36 rows=50000 width=45)  
   Hash Cond: (a.id = b.id)  
   Filter: (b.* IS NULL)  
   CTE t1  
     ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
   ->  Seq Scan on a  (cost=0.00..193458.06 rows=10000006 width=45)  
   ->  Hash  (cost=0.10..0.10 rows=5 width=32)  
         ->  CTE Scan on t1 b  (cost=0.00..0.10 rows=5 width=32)  
(8 rows)  
  
postgres=# explain select id from a except select id from (values (1),(2),(3),(4),(5)) as t(id);  
                                          QUERY PLAN  
-----------------------------------------------------------------------------------------------  
 SetOp Except  (cost=1538166.72..1588166.78 rows=10000006 width=8)  
   ->  Sort  (cost=1538166.72..1563166.75 rows=10000011 width=8)  
         Sort Key: "*SELECT* 1".id  
         ->  Append  (cost=0.00..293458.23 rows=10000011 width=8)  
               ->  Subquery Scan on "*SELECT* 1"  (cost=0.00..293458.12 rows=10000006 width=8)  
                     ->  Seq Scan on a  (cost=0.00..193458.06 rows=10000006 width=4)  
               ->  Subquery Scan on "*SELECT* 2"  (cost=0.00..0.11 rows=5 width=8)  
                     ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
(8 rows)  

2、設計

1億記錄,查詢匹配多個輸入值的效能。分別輸入1,10,100,1000,10000,100000,1000000個值作為匹配條件。

1、not in (...)

2、not in (table or subquery or srf)

3、<> all (array)

4、not exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

5、<>? and <>? and <>? and .....

6、left join others b on (a.?=b.?) where b.* is null

7、select ? from a except select ? from b,適用於輸出欄位與條件欄位相同的情形。

3、準備測試表

create table t_in_test (id int primary key, info text, crt_time timestamp);  

4、準備測試函式(可選)

5、準備測試資料

insert into t_in_test select generate_series(1,100000000), md5(random()::text), clock_timestamp();  

6、準備測試指令碼

set parallel_setup_cost =0;  
set parallel_tuple_cost =0;  
set max_parallel_workers_per_gather =28;  
alter table t_in_test set (parallel_workers =28);  

1、not in (...)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

do language plpgsql $$  
declare  
  arr text;  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    select string_agg((random()*100000)::int::text, `,`) into arr from generate_series(1, mx);  
    ts := clock_timestamp();  
    execute `select * from t_in_test where id not in (`||arr||`)`;  
    raise notice `%: %`, mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

2、not in (table or subquery or srf)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

do language plpgsql $$  
declare  
  arr text;  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform * from t_in_test where not id in ( select (random()*100000)::int from generate_series(1, mx) );  
    raise notice `%: %`, mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

3、<> all (array)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

do language plpgsql $$  
declare  
  arr int[];  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    select array_agg((random()*100000)::int) into arr from generate_series(1, mx);  
    ts := clock_timestamp();  
    perform * from t_in_test where id <> all ( arr );  
    raise notice `%: %`, mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

4、not exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

do language plpgsql $$  
declare  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform * from t_in_test where not exists ( select 1 from ( select (random()*100000)::int id from generate_series(1,mx) ) t where t_in_test.id=t.id );  
    raise notice `%: %`, mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

6、left join others b on (a.?=b.?) where b.* is null

do language plpgsql $$  
declare  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform a.* from t_in_test a left join (select (random()*100000)::int id from generate_series(1,mx)) b on (a.id=b.id) where b.* is null;  
    raise notice `%: %`, mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

7、select ? from a except select ? from b,適用於輸出欄位與條件欄位相同的情形。

do language plpgsql $$  
declare  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform a.id from t_in_test a except select (random()*100000)::int id from generate_series(1,mx);  
    raise notice `%: %`, mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

7、測試

1、not in (...)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

NOTICE:  1: 00:00:20.760034  
NOTICE:  10: 00:00:27.766224  
NOTICE:  100: 00:01:22.95002  
NOTICE:  1000: 00:10:16.690793  
..........  
10000開始很久也沒跑出來。繼續看後面其他方法的測試。  

2、not in (table or subquery or srf)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

-----  

3、<> all (array)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

-----  

4、not exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

NOTICE:  1: 00:00:35.253582  
NOTICE:  10: 00:00:35.256638  
NOTICE:  100: 00:00:35.164034  
NOTICE:  1000: 00:00:35.417756  
NOTICE:  10000: 00:00:35.205454  
NOTICE:  100000: 00:00:35.458987  
NOTICE:  1000000: 00:00:35.447743  
DO  

6、a left join others b on (a.?=b.?) where b.* is null

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

NOTICE:  1: 00:00:36.474715  
NOTICE:  10: 00:00:36.53191  
NOTICE:  100: 00:00:36.60439  
NOTICE:  1000: 00:00:36.534846  
NOTICE:  10000: 00:00:36.574136  
NOTICE:  100000: 00:00:36.519582  
NOTICE:  1000000: 00:00:37.675594  
DO  

7、select ? from a except select ? from b,適用於輸出欄位與條件欄位相同的情形。

1,10,100,1000,10000,100000,1000000 個輸入值的測試效能

NOTICE:  1: 00:00:50.566741  
NOTICE:  10: 00:00:50.051715  
NOTICE:  100: 00:00:50.098839  
NOTICE:  1000: 00:00:49.966196  
NOTICE:  10000: 00:00:50.608288  
NOTICE:  100000: 00:00:50.715218  
NOTICE:  1000000: 00:00:51.794935  
DO  

TPS

平均響應時間
not exists為例,1億記錄1到100萬個點的排他過濾。
NOTICE: 1: 00:00:35.253582
NOTICE: 10: 00:00:35.256638
NOTICE: 100: 00:00:35.164034
NOTICE: 1000: 00:00:35.417756
NOTICE: 10000: 00:00:35.205454
NOTICE: 100000: 00:00:35.458987
NOTICE: 1000000: 00:00:35.447743
DO

參考

《PostgreSQL、Greenplum 應用案例寶典《如來神掌》 – 目錄》

《資料庫選型之 – 大象十八摸 – 致 架構師、開發者》

《PostgreSQL 使用 pgbench 測試 sysbench 相關case》

《資料庫界的華山論劍 tpc.org》

https://www.postgresql.org/docs/10/static/pgbench.html


相關文章