閱讀135 返回首頁    go 阿裏雲 go 技術社區[雲棲]


HTAP數據庫 PostgreSQL 場景與性能測試之 25 - (OLTP) IN , EXISTS 查詢

標簽

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來估算。

場景 - IN , EXISTS 查詢 (OLTP)

1、背景

in 查詢,多用在多個輸入值的匹配場景。

實際上PostgreSQL支持很多種多個輸入值匹配的語法。

1、in (...)

2、in (table or subquery or srf)

3、= any (array)

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

5、=? or =? or =? or .....

他們的執行計劃分別如下,(in (values....) or = any (array)最佳) :

postgres=# explain select * from a where id in (1,2,3,4,5);  
                           QUERY PLAN                              
-----------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=0.43..9.46 rows=5 width=45)  
   Index Cond: (id = ANY ('{1,2,3,4,5}'::integer[]))  
(2 rows)  
  
postgres=# explain select * from a where id = any (array[1,2,3,4,5]);  
                           QUERY PLAN                              
-----------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=0.43..9.46 rows=5 width=45)  
   Index Cond: (id = ANY ('{1,2,3,4,5}'::integer[]))  
(2 rows)  
  
postgres=# explain select * from a where id = any (array(select generate_series(1,10)));  
                            QUERY PLAN                               
-------------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=5.45..22.74 rows=10 width=45)  
   Index Cond: (id = ANY ($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 = any (array(select id from (values (1),(2),(3),(4),(5)) t (id)));  
                             QUERY PLAN                                
---------------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=0.50..17.79 rows=10 width=45)  
   Index Cond: (id = ANY ($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 in (select id from (values (1),(2),(3),(4),(5)) t (id));  
                               QUERY PLAN                                  
-------------------------------------------------------------------------  
 Nested Loop  (cost=0.51..14.39 rows=5 width=45)  
   ->  HashAggregate  (cost=0.07..0.12 rows=5 width=4)  
         Group Key: "*VALUES*".column1  
         ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
   ->  Index Scan using a_pkey on a  (cost=0.43..2.85 rows=1 width=45)  
         Index Cond: (id = "*VALUES*".column1)  
(6 rows)  
  
postgres=# explain select * from a where exists (select 1 from (values (1),(2),(3),(4),(5)) t (id) where t.id=a.id);  
                               QUERY PLAN                                  
-------------------------------------------------------------------------  
 Nested Loop  (cost=0.51..14.39 rows=5 width=45)  
   ->  HashAggregate  (cost=0.07..0.12 rows=5 width=4)  
         Group Key: "*VALUES*".column1  
         ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
   ->  Index Scan using a_pkey on a  (cost=0.43..2.85 rows=1 width=45)  
         Index Cond: (id = "*VALUES*".column1)  
(6 rows)  
  
postgres=# explain select * from a where id=1 or id=2 or id=3 or id=4 or id =5;  
                                 QUERY PLAN                                   
----------------------------------------------------------------------------  
 Bitmap Heap Scan on a  (cost=8.22..14.32 rows=5 width=45)  
   Recheck Cond: ((id = 1) OR (id = 2) OR (id = 3) OR (id = 4) OR (id = 5))  
   ->  BitmapOr  (cost=8.22..8.22 rows=5 width=0)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 1)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 2)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 3)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 4)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 5)  
(13 rows)  

2、設計

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

1、in (...)

2、in (table or subquery or srf)

3、= any (array)

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

5、=? or =? or =? or .....

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、準備測試腳本

1、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 in ('||arr||')';  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

2、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 id in ( select (random()*100000)::int from generate_series(1, mx) );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

3、= any (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 = any ( arr );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

4、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 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;  
$$ ;  

5、壓測

匹配1 ~ 100個輸入值,求聚合。高並發。

vi test.sql  
  
\set x random(1,100)  
select count(*) from t_in_test where id = any(array(select (random()*100000000)::int from generate_series(1,:x)));  

壓測

CONNECTS=56    
TIMES=300    
export PGHOST=$PGDATA    
export PGPORT=1999    
export PGUSER=postgres    
export PGPASSWORD=postgres    
export PGDATABASE=postgres    
    
pgbench -M prepared -n -r -f ./test.sql -P 5 -c $CONNECTS -j $CONNECTS -T $TIMES    

7、測試

1、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 in ('||arr||')';  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.000256  
NOTICE:  10: 00:00:00.000173  
NOTICE:  100: 00:00:00.000772  
NOTICE:  1000: 00:00:00.004445  
NOTICE:  10000: 00:00:00.024073  
NOTICE:  100000: 00:00:00.195439  
NOTICE:  1000000: 00:00:01.638982  
DO  

2、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 id in ( select (random()*100000)::int from generate_series(1, mx) );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.00044  
NOTICE:  10: 00:00:00.000244  
NOTICE:  100: 00:00:00.000788  
NOTICE:  1000: 00:00:00.004455  
NOTICE:  10000: 00:00:00.028793  
NOTICE:  100000: 00:00:00.187841  
NOTICE:  1000000: 00:00:00.583744  
DO  

3、= any (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 = any ( arr );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.000216  
NOTICE:  10: 00:00:00.000151  
NOTICE:  100: 00:00:00.000654  
NOTICE:  1000: 00:00:00.00399  
NOTICE:  10000: 00:00:00.021216  
NOTICE:  100000: 00:00:00.106335  
NOTICE:  1000000: 00:00:00.386113  
DO  

4、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 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;  
$$ ;  
NOTICE:  1: 00:00:00.000458
NOTICE:  10: 00:00:00.000224
NOTICE:  100: 00:00:00.000687
NOTICE:  1000: 00:00:00.003916
NOTICE:  10000: 00:00:00.02734
NOTICE:  100000: 00:00:00.187671
NOTICE:  1000000: 00:00:00.570389
DO

5、匹配1 ~ 100個輸入值,求聚合。高並發。

transaction type: ./test.sql  
scaling factor: 1  
query mode: prepared  
number of clients: 56  
number of threads: 56  
duration: 300 s  
number of transactions actually processed: 13913566  
latency average = 1.207 ms  
latency stddev = 0.840 ms  
tps = 46378.142149 (including connections establishing)  
tps = 46384.723274 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set x random(1,100)  
         1.207  select count(*) from t_in_test where id = any(array(select (random()*100000000)::int from generate_series(1,:x)));  

TPS: 46384

5、匹配1 ~ 100個輸入值,求聚合。高並發。

平均響應時間: 1.207 毫秒

5、匹配1 ~ 100個輸入值,求聚合。高並發。

1到100萬個輸入值的響應時間

1億條記錄,匹配100萬個輸入值( = any (array) ),隻需要386毫秒。

NOTICE:  1: 00:00:00.000216  
NOTICE:  10: 00:00:00.000151  
NOTICE:  100: 00:00:00.000654  
NOTICE:  1000: 00:00:00.00399  
NOTICE:  10000: 00:00:00.021216  
NOTICE:  100000: 00:00:00.106335  
NOTICE:  1000000: 00:00:00.386113  

參考

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

《數據庫選型之 - 大象十八摸 - 致 架構師、開發者》

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

《數據庫界的華山論劍 tpc.org》

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

最後更新:2017-11-14 14:04:40

  上一篇:go  HTAP數據庫 PostgreSQL 場景與性能測試之 26 - (OLTP) NOT IN、NOT EXISTS 查詢
  下一篇:go  HTAP數據庫 PostgreSQL 場景與性能測試之 24 - (OLTP) 物聯網 - 時序數據並發寫入(含時序索引BRIN)