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


HTAP數據庫 PostgreSQL 場景與性能測試之 24 - (OLTP) 物聯網 - 時序數據並發寫入(含時序索引BRIN)

標簽

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

場景 - 物聯網 - 時序數據並發寫入(含時序索引BRIN) (OLTP)

1、背景

物聯網數據,並發量大,寫入吞吐大,但是時序屬性,按時間區間查詢、聚合、過濾、流式處理的需求最為旺盛。

PostgreSQL的時序索引(也可以稱為塊級索引),索引小,但是對於時序數據的過濾性特別好,並且幾乎不影響寫入效率。

2、設計

1、單表,含時序索引,單條並發寫入。

2、多表,含時序索引,單條並發寫入。

3、單表,含時序索引,批量並發寫入。

4、多表,含時序索引,批量並發寫入。

3、準備測試表

包含索引。

create table feed (id int, val float, crt_time timestamp default now());  
create index idx_feed on feed using BRIN (crt_time) tablespace tbs1;  
  
do language plpgsql $$  
declare  
begin  
  for i in 1..1024 loop  
    execute 'create table feed'||i||' (like feed including all)';  
  end loop;  
end;  
$$;  

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

動態SQL,寫入不同分表。

create or replace function ins_batch(int, int) returns void as $$  
declare  
begin  
  execute 'insert into feed'||$1||' select id , 0.1 from generate_series(1,'||$2||') t(id)';  
end;  
$$ language plpgsql strict;  
  
create or replace function ins(int) returns void as $$  
declare  
begin  
  execute 'insert into feed'||$1||' values (1, 0.1)';  
end;  
$$ language plpgsql strict;  

5、準備測試數據

6、準備測試腳本

1、單表,含時序索引,單條並發寫入。

vi test.sql  
  
insert into feed (id, val) values (1,0.1);  

2、多表,含時序索引,單條並發寫入。

vi test.sql  
  
\set suffix random(1,1024)  
select ins(:suffix)  

3、單表,含時序索引,批量並發寫入。

vi test.sql  
  
insert into feed (id, val) select 1, 0.1 from generate_series(1,1000);  

4、多表,含時序索引,批量並發寫入。

vi test.sql  
  
\set suffix random(1,1024)  
select ins_batch(:suffix, 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、測試

1、單表,含時序索引,單條並發寫入。

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: 81975309  
latency average = 0.205 ms  
latency stddev = 0.351 ms  
tps = 273236.057797 (including connections establishing)  
tps = 273259.238238 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.205  insert into feed (id, val) values (1,0.1);  

2、多表,含時序索引,單條並發寫入。

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: 52089776  
latency average = 0.322 ms  
latency stddev = 0.267 ms  
tps = 173584.822070 (including connections establishing)  
tps = 173612.564907 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set suffix random(1,1024)  
         0.321  select ins(:suffix)  

3、單表,含時序索引,批量並發寫入。

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: 605700  
latency average = 27.735 ms  
latency stddev = 25.144 ms  
tps = 2018.830266 (including connections establishing)  
tps = 2019.002544 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
        27.735  insert into feed (id, val) select 1, 0.1 from generate_series(1,1000);  

4、多表,含時序索引,批量並發寫入。

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: 941248
latency average = 17.847 ms
latency stddev = 27.876 ms
tps = 3137.373893 (including connections establishing)
tps = 3137.648888 (excluding connections establishing)
script statistics:
 - statement latencies in milliseconds:
         0.002  \set suffix random(1,1024)
        17.846  select ins_batch(:suffix, 1000)

TPS

1、單表,含時序索引,單條並發寫入。TPS: 273259 。

2、多表,含時序索引,單條並發寫入。TPS: 173612 。

3、單表,含時序索引,批量並發寫入。TPS: 2019 。相當於每秒寫入 201.9萬 條記錄。

4、多表,含時序索引,批量並發寫入。TPS: 3137 。相當於每秒寫入 313.7萬 條記錄。

平均響應時間

1、單表,含時序索引,單條並發寫入。0.205 毫秒。

2、多表,含時序索引,單條並發寫入。0.322 毫秒。

3、單表,含時序索引,批量並發寫入。27.735 毫秒。

4、多表,含時序索引,批量並發寫入。17.847 毫秒。

參考

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

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

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

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

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

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

  上一篇:go  HTAP數據庫 PostgreSQL 場景與性能測試之 25 - (OLTP) IN , EXISTS 查詢
  下一篇:go  工欲善其事必先利其器 —— 配置vim