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


HTAP數據庫 PostgreSQL 場景與性能測試之 41 - (OLTP+OLAP) 含索引多表批量寫入

標簽

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

場景 - 含索引多表批量寫入 (OLTP+OLAP)

1、背景

含索引,多表(128個表),每次寫入多條記錄。這是非常典型的測試TP或AP場景,數據實時灌入場景的能力。

2、設計

多表(128個表),含索引,單事務多條寫入(一次寫入1000條)。高並發。

3、準備測試表

create table t_sensor(    
  id int8,    
  c1 int8 default 0,    
  c2 int8 default 0,    
  c3 int8 default 0,    
  c4 float8 default 0,    
  c5 text default 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',    
  ts timestamp default clock_timestamp()    
) with (autovacuum_enabled=off, toast.autovacuum_enabled=off);    
    
create index idx_t_sensor_ts on t_sensor using btree (ts) tablespace tbs1;    
do language plpgsql $$    
declare    
begin    
  for i in 1..128 loop    
    execute format('create table t_sensor%s (like t_sensor including all) inherits (t_sensor) with (autovacuum_enabled=off, toast.autovacuum_enabled=off)', i);    
  end loop;    
end;    
$$;    

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

create or replace function ins_sensor(int, int) returns void as $$    
declare    
begin    
  execute format('insert into t_sensor%s (id) select generate_series(1,%s)', $1, $2);    
  -- 為了拚接表名,使用了動態SQL,硬解析耗時。    
  -- 導致測試結果有出入,至少不會比單表無索引寫入性能差。    
  -- 批量寫入的話,硬解析的問題可以被掩蓋。    
end;    
$$ language plpgsql strict;    

5、準備測試數據

6、準備測試腳本

vi test.sql    
    
\set sid random(1,128)    
select ins_sensor(:sid, 1000);    

壓測

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、測試

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: 435228  
latency average = 38.598 ms  
latency stddev = 16.124 ms  
tps = 1450.560412 (including connections establishing)  
tps = 1450.687176 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set sid random(1,128)    
        38.599  select ins_sensor(:sid, 1000);  

TPS: 1450 ( = 145萬 行/s )

多表(128個表),含索引,單事務多條寫入(一次寫入1000條)。高並發。

主要瓶頸:b-tree lock, xlog lock.

平均響應時間: 38.598 毫秒

多表(128個表),含索引,單事務多條寫入(一次寫入1000條)。高並發。

主要瓶頸:b-tree lock, xlog lock.

參考

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

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

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

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

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

最後更新:2017-11-19 14:34:56

  上一篇:go  HTAP數據庫 PostgreSQL 場景與性能測試之 42 - (OLTP+OLAP) unlogged table 不含索引多表批量寫入
  下一篇:go  HTAP數據庫 PostgreSQL 場景與性能測試之 40 - (OLTP+OLAP) 不含索引多表批量寫入