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


HTAP數據庫 PostgreSQL 場景與性能測試之 20 - (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來估算。

場景 - 用戶畫像圈人場景 - 多個字段任意組合條件篩選與透視 (OLAP)

1、背景

用戶畫像表有多個字段,表示不同類型的標簽屬性,在進行人群圈選時,需要對任意字段的組合條件進行條件篩選,並對人群結果進行透視。

PostgreSQL 有3種方法實現多個字段的任意組合過濾。

1、布隆過濾,支持任意字段組合的等值查詢。

《PostgreSQL 9.6 黑科技 bloom 算法索引,一個索引支撐任意列組合查詢》

2、多索引 bitmap scan

gin複合索引,或者多個b-tree單列索引,都可以實現bitmap scan。

當輸入多個條件時,過濾、收斂到更少的數據塊,順序掃描+FILTER。

《PostgreSQL bitmapAnd, bitmapOr, bitmap index scan, bitmap heap scan》

3、GIN複合索引 bitmap scan

當輸入多個條件時,過濾、收斂到更少的數據塊,順序掃描+FILTER。

《寶劍贈英雄 - 任意組合字段等效查詢, 探探PostgreSQL多列展開式B樹 (GIN)》

2、設計

1億條記錄,每條記錄包含32個標簽字段,每個字段的標簽取值範圍1萬。另外包含3個屬性字段用於透視。

3、準備測試表

do language plpgsql $$  
declare  
  sql text;  
begin  
  sql := 'create table t_multi_col (id int8, c1 int default random()*100, c2 int default random()*10, c3 int default random()*10, ';  
  for i in 4..35 loop  
    sql := sql||'c'||i||' int default random()*10000,';  
  end loop;  
  sql := rtrim(sql, ',');  
  sql := sql||')';  
  execute sql;  
end;  
$$;  

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

5、準備測試數據

insert into t_multi_col (id) select generate_series(1,100000000);  

1、布隆索引

create extension bloom;  
  
do language plpgsql $$  
declare  
  sql text;  
begin  
  sql := 'create index idx_t_multi_col on t_multi_col using bloom (';  
  for i in 4..35 loop  
    sql := sql||'c'||i||',';  
  end loop;  
  sql := rtrim(sql, ',');  
  sql := sql||') with (length=80, ';  
  for i in 1..32 loop  
    sql := sql||'col'||i||'=2,';  
  end loop;  
  sql := rtrim(sql, ',');  
  sql := sql||')';  
  execute sql;  
end;  
$$;  

2、GIN索引

create extension btree_gin;  
  
do language plpgsql $$  
declare  
  sql text;  
begin  
  sql := 'create index idx_t_multi_col_gin on t_multi_col using gin (';  
  for i in 4..35 loop  
    sql := sql||'c'||i||',';  
  end loop;  
  sql := rtrim(sql, ',');  
  sql := sql||')';  
  execute sql;  
end;  
$$;  

6、準備測試腳本

vi test.sql  
  
\set a4 random(1,10000)  
\set a5 random(1,10000)  
\set a6 random(1,10000)  
\set a7 random(1,10000)  
\set a8 random(1,10000)  
\set a9 random(1,10000)  
select c1,c2,c3,count(*) from t_multi_col where c4=:a4 and c5=:a5 and c6=:a6 and c7=:a7 and c8=:a8 and c9=:a9 group by grouping sets ((c1),(c2),(c3));  

7、測試

1、布隆索引,由於需要掃整個索引,耗時略高。500毫秒。

postgres=# explain (analyze,verbose,timing,costs,buffers) select c1,c2,c3,count(*) from t_multi_col where c4=3 and c5=2 and c6=1 and c7=4 and c8=5 and c9=6 and c10=1 and c11=1 and c12=1 group by grouping sets ((c1),(c2),(c3));  
                                                      QUERY PLAN  
-----------------------------------------------------------------------------------------------------------------------  
 HashAggregate  (cost=2985297.24..2985297.28 rows=3 width=20) (actual time=499.961..499.961 rows=0 loops=1)  
   Output: c1, c2, c3, count(*)  
   Hash Key: t_multi_col.c1  
   Hash Key: t_multi_col.c2  
   Hash Key: t_multi_col.c3  
   Buffers: shared hit=197418  
   ->  Bitmap Heap Scan on public.t_multi_col  (cost=2985296.00..2985297.23 rows=1 width=12) (actual time=499.958..499.958 rows=0 loops=1)  
         Output: id, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35  
         Recheck Cond: ((t_multi_col.c4 = 3) AND (t_multi_col.c5 = 2) AND (t_multi_col.c6 = 1) AND (t_multi_col.c7 = 4) AND (t_multi_col.c8 = 5) AND (t_multi_col.c9 = 6) AND (t_multi_col.c10 = 1) AND (t_multi_col.c11 = 1) AND (t_multi_col.c12 = 1))  
         Rows Removed by Index Recheck: 1339  
         Heap Blocks: exact=1339  
         Buffers: shared hit=197418  
         ->  Bitmap Index Scan on idx_t_multi_col  (cost=0.00..2985296.00 rows=1 width=0) (actual time=497.718..497.718 rows=1339 loops=1)  
               Index Cond: ((t_multi_col.c4 = 3) AND (t_multi_col.c5 = 2) AND (t_multi_col.c6 = 1) AND (t_multi_col.c7 = 4) AND (t_multi_col.c8 = 5) AND (t_multi_col.c9 = 6) AND (t_multi_col.c10 = 1) AND (t_multi_col.c11 = 1) AND (t_multi_col.c12 = 1))  
               Buffers: shared hit=196079  
 Planning time: 0.165 ms  
 Execution time: 500.025 ms  
(17 rows)  

2、gin索引,精準定位,耗時2毫秒以內。

postgres=# explain (analyze,verbose,timing,costs,buffers) select c1,c2,c3,count(*) from t_multi_col where c4=3 and c5=2 and c6=1 and c7=4 and c8=5 and c9=6 and c10=1 and c11=1 and c12=1 group by grouping sets ((c1),(c2),(c3));  
                                  QUERY PLAN  
--------------------------------------------------------------------------------------------------------------------------------  
 HashAggregate  (cost=69.64..69.68 rows=3 width=20) (actual time=1.151..1.151 rows=0 loops=1)  
   Output: c1, c2, c3, count(*)  
   Hash Key: t_multi_col.c1  
   Hash Key: t_multi_col.c2  
   Hash Key: t_multi_col.c3  
   Buffers: shared hit=69  
   ->  Bitmap Heap Scan on public.t_multi_col  (cost=68.40..69.63 rows=1 width=12) (actual time=1.149..1.149 rows=0 loops=1)  
         Output: id, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35  
         Recheck Cond: ((t_multi_col.c4 = 3) AND (t_multi_col.c5 = 2) AND (t_multi_col.c6 = 1) AND (t_multi_col.c7 = 4) AND (t_multi_col.c8 = 5) AND (t_multi_col.c9 = 6) AND (t_multi_col.c10 = 1) AND (t_multi_col.c11 = 1) AND (t_multi_col.c12 = 1))  
         Buffers: shared hit=69  
         ->  Bitmap Index Scan on idx_t_multi_col_gin  (cost=0.00..68.40 rows=1 width=0) (actual time=1.146..1.146 rows=0 loops=1)  
               Index Cond: ((t_multi_col.c4 = 3) AND (t_multi_col.c5 = 2) AND (t_multi_col.c6 = 1) AND (t_multi_col.c7 = 4) AND (t_multi_col.c8 = 5) AND (t_multi_col.c9 = 6) AND (t_multi_col.c10 = 1) AND (t_multi_col.c11 = 1) AND (t_multi_col.c12 = 1))  
               Buffers: shared hit=69  
 Planning time: 0.263 ms  
 Execution time: 1.245 ms  
(15 rows)  

壓測

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  

8、測試結果

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: 10740407  
latency average = 1.564 ms  
latency stddev = 0.561 ms  
tps = 35796.375710 (including connections establishing)  
tps = 35800.169989 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set a4 random(1,10000)  
         0.000  \set a5 random(1,10000)  
         0.000  \set a6 random(1,10000)  
         0.000  \set a7 random(1,10000)  
         0.000  \set a8 random(1,10000)  
         0.000  \set a9 random(1,10000)  
         1.562  select c1,c2,c3,count(*) from t_multi_col where c4=:a4 and c5=:a5 and c6=:a6 and c7=:a7 and c8=:a8 and c9=:a9 group by grouping sets ((c1),(c2),(c3));  

TPS: 35800

平均響應時間: 1.564 毫秒

實際上,除了BITMAPSCAN,還有一種存儲層優化,目前PostgreSQL內部引擎為行存儲引擎,通過插件支持列存儲,列存儲優化可以減少掃描的數據塊的數量,提高性能。

參考

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

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

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

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

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

最後更新:2017-11-12 02:06:18

  上一篇:go  PostgreSQL Oracle 兼容性之 - rowid (CREATE TABLE WITH OIDS)
  下一篇:go  HTAP數據庫 PostgreSQL 場景與性能測試之 19 - (OLAP) 用戶畫像圈人場景 - 數組相交查詢與聚合