558
技術社區[雲棲]
HTAP數據庫 PostgreSQL 場景與性能測試之 16 - (OLTP) 文本特征向量 - 相似特征(海明...)查詢
標簽
PostgreSQL , HTAP , OLTP , OLAP , 場景與性能測試
背景
PostgreSQL是一個曆史悠久的數據庫,曆史可以追溯到1973年,最早由2014計算機圖靈獎得主,關係數據庫的鼻祖Michael_Stonebraker 操刀設計,PostgreSQL具備與Oracle類似的功能、性能、架構以及穩定性。
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流計算、分布式並行計算、時序處理、基因測序、化學分析、圖像分析 等。
在各種應用場景中都可以看到PostgreSQL的應用:
PostgreSQL近年來的發展非常迅勐,從知名數據庫評測網站dbranking的數據庫評分趨勢,可以看到PostgreSQL向上發展的趨勢:
從每年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)
1、背景
對於長文本來說、或者一些較長文本來說,如果要搜索語義相似的文本,使用全文檢索、模煳查詢都不太合適,無法滿足需求。
通常的做法是提取文本的特征詞,根據特征來搜索相似的文本。
比如求不同文本之間的海明距離,得到的距離越近,越相似。
《海量數據,海明(simhash)距離高效檢索(smlar) - 阿裏雲RDS PosgreSQL最佳實踐》
2、設計
1億條文本特征向量的海明碼,輸入任意海明碼,求與之相似的記錄。
1億個海明碼,搜索與指定海明碼的距離在3以內的記錄。
3、準備測試表
create extension smlar;
create table hm3 (id int, hmval bit(64), hmarr text[]);
4、準備測試函數(可選)
生成隨機海明碼的函數
create or replace function gen_rand_bit() returns bit(64) as $$
select (sqrt(random())::numeric*9223372036854775807*2-9223372036854775807::numeric)::int8::bit(64);
$$ language sql strict;
create or replace function gen_arr(text) returns text[] as $$
select regexp_split_to_array('1_'||substring($1,1,16)||',2_'||substring($1,17,16)||',3_'||substring($1,33,16)||',4_'||substring($1,41,16), ',') ;
$$ language sql strict;
測試搜索與指定海明碼的距離在3以內的記錄的函數
create or replace function f_test () returns setof record as $$
declare
ts text;
arr text[];
begin
set smlar.type = overlap;
set smlar.threshold = 3;
set LOCAL enable_seqscan=off;
select gen_rand_bit()::text into ts;
select gen_arr(ts) into arr;
return query select
*,
smlar( hmarr, arr)
from
hm3
where
hmarr % arr
and length(replace(bitxor(ts::bit(64), hmval)::text,'0','')) < 2
limit 1;
end;
$$ language plpgsql strict;
5、準備測試數據
insert into hm3
select
id,
val::bit(64),
regexp_split_to_array('1_'||substring(val,1,16)||',2_'||substring(val,17,16)||',3_'||substring(val,33,16)||',4_'||substring(val,41,16), ',')
from
(select id, (sqrt(random())::numeric*9223372036854775807*2-9223372036854775807::numeric)::int8::bit(64)::text as val from generate_series(1,100000000) t(id)) t;
create index idx_hm3 on hm3 using gin(hmarr _text_sml_ops );
6、準備測試腳本
vi test.sql
select * from f_test() as t(id int, hmval bit(64), hmarr text[], dist real);
7、測試
單次相似查詢效率,響應時間低於 2 毫秒。(使用綁定變量、並且CACHE命中後,響應時間更低。)
select
*,
smlar( hmarr, '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[])
from
hm3
where
hmarr % '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[]
and length(replace(bitxor('0000000010010010110011011001010011011101000110111111111001111111'::bit(64), hmval)::text,'0','')) < 2
limit 1;
id | hmval | hmarr | smlar
----+------------------------------------------------------------------+-------------------------------------------------------------------------------+-------
1 | 0000000010010010110011011001010011011101000110111111111001111110 | {1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111110} | 3
(1 row)
Time: 1.335 ms
postgres=# explain (analyze,verbose,timing,costs,buffers) select
*,
smlar( hmarr, '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[])
from
hm3
where
hmarr % '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[]
and length(replace(bitxor('0000000010010010110011011001010011011101000110111111111001111111'::bit(64), hmval)::text,'0','')) < 2
limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=811.33..814.35 rows=1 width=138) (actual time=0.563..0.563 rows=1 loops=1)
Output: id, hmval, hmarr, (smlar(hmarr, '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[]))
Buffers: shared hit=19
-> Bitmap Heap Scan on public.hm3 (cost=811.33..101253.67 rows=33333 width=138) (actual time=0.561..0.561 rows=1 loops=1)
Output: id, hmval, hmarr, smlar(hmarr, '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[])
Recheck Cond: (hm3.hmarr % '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[])
Filter: (length(replace((bitxor(B'0000000010010010110011011001010011011101000110111111111001111111'::bit(64), hm3.hmval))::text, '0'::text, ''::text)) < 2)
Heap Blocks: exact=1
Buffers: shared hit=19
-> Bitmap Index Scan on idx_hm3 (cost=0.00..803.00 rows=100000 width=0) (actual time=0.538..0.538 rows=1 loops=1)
Index Cond: (hm3.hmarr % '{1_0000000010010010,2_1100110110010100,3_1101110100011011,4_0001101111111111}'::text[])
Buffers: shared hit=18
Planning time: 0.134 ms
Execution time: 0.602 ms
(14 rows)
Time: 1.269 ms
壓測
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: 14721374
latency average = 1.140 ms
latency stddev = 0.590 ms
tps = 49053.614018 (including connections establishing)
tps = 49054.615079 (excluding connections establishing)
script statistics:
- statement latencies in milliseconds:
1.139 select * from f_test() as t(id int, hmval bit(64), hmarr text[], dist real);
TPS: 49054
平均響應時間: 1.140 毫秒
參考
《PostgreSQL、Greenplum 應用案例寶典《如來神掌》 - 目錄》
《PostgreSQL 使用 pgbench 測試 sysbench 相關case》
https://www.postgresql.org/docs/10/static/pgbench.html
最後更新:2017-11-12 02:05:50