PostgreSQL 多重含義陣列檢索與條件過濾 (標籤1:屬性, 標籤n:屬性)
摘要: 標籤 PostgreSQL , 多重函式陣列 , UDF索引 , 過濾 , 文字處理 背景 PG的陣列型別,被廣泛應用於 畫像系統 , 標籤系統。 在一些業務重建中,對陣列內容的定義往往包含了多重含義,例如即包含了標籤本身,又包含了標籤的屬性(例如 標籤值:權值,時間 等)。
標籤
PostgreSQL , 多重函式陣列 , UDF索引 , 過濾 , 文字處理
背景
PG的陣列型別,被廣泛應用於 畫像系統 , 標籤系統。
在一些業務重建中,對陣列內容的定義往往包含了多重含義,例如即包含了標籤本身,又包含了標籤的屬性(例如 標籤值:權值,時間 等)。
那麼如何能高效的進行標籤的檢索,同時又過濾出符合標籤加權值的記錄呢?
例子
1、建表
create table tbl(id int, info text[]);
2、寫入測試資料
insert into tbl values (1, array[‘a:100’, ‘b:10’]);
insert into tbl values (2, array[‘a:15’, ‘b:20’, ‘c:99’]);
insert into tbl values (3, array[‘c:78’, ‘b:100’]);
postgres=# select * from tbl;
id | info
—-+——————
1 | {a:100,b:10}
2 | {a:15,b:20,c:99}
3 | {c:78,b:100}
(3 rows)
3、建立UDF1,提取出要查詢的標籤值(用到了正則匹配)
create or replace function get_label(text[]) returns text[] as
selectarray(selectsubstring(unnest($1),′(.∗):′)); language sql strict immutable;postgres=# select get_label(info) from tbl;
get_label
{a,b}
{a,b,c}
{c,b}
(3 rows)
4、建立UDF1索引
create index idx_tbl1 on tbl using gin (get_label(info));
postgres=# explain select * from tbl where get_label(info) @> array[‘a’];
QUERY PLAN
Bitmap Heap Scan on tbl (cost=2.40..3.86 rows=1 width=36)
Recheck Cond: (get_label(info) @> ‘{a}’::text[])
-> Bitmap Index Scan on idx_tbl1 (cost=0.00..2.40 rows=1 width=0)
Index Cond: (get_label(info) @> ‘{a}’::text[])
(4 rows)
5、建立UDF2,提取指定標籤的加權值(用到了正則匹配,陣列下標計算,陣列按位置取元素等操作)
create or replace function get_weight(text[], text) returns text as
selectsubstring($1[arrayposition(getlabel($1),$2)],′:(.∗)′); language sql strict immutable;postgres=# select info, get_weight(info, ‘a’) from tbl;
info | get_weight
——————+————
{a:100,b:10} | 100
{a:15,b:20,c:99} | 15
{c:78,b:100} |
(3 rows)
6、查詢SQL如下
查詢包含標籤a,同時權值大於20的記錄。
postgres=# select * from tbl where get_label(info) @> array[‘a’] and get_weight(info, ‘a’)::float8 >20;
id | info
—-+————–
1 | {a:100,b:10}
(1 row)
postgres=# explain select * from tbl where get_label(info) @> array[‘a’] and get_weight(info, ‘a’)::float8 >20;
QUERY PLAN
Bitmap Heap Scan on tbl (cost=2.40..4.12 rows=1 width=36)
Recheck Cond: (get_label(info) @> ‘{a}’::text[])
Filter: ((get_weight(info, ‘a’::text))::double precision > ‘20’::double precision)
-> Bitmap Index Scan on idx_tbl1 (cost=0.00..2.40 rows=1 width=0)
Index Cond: (get_label(info) @> ‘{a}’::text[])
(5 rows)
UDF功能是不是很贊呢?
1、追加元素
array_append(anyarray, anyelement)
array_cat(anyarray, anyarray)
array_fill(anyelement, int[], [, int[]])
array_prepend(anyelement, anyarray)
2、修改元素
array_replace(anyarray, anyelement, anyelement)
3、刪除元素
array_remove(anyarray, anyelement)
用法舉例
insert into tbl values (1, ?) on conflict (id) do update set info=func(tbl.info,?);
create table tbl1(id int primary key, info int[]);
postgres=# insert into tbl1 values (1, array[1,2,3]) on conflict (id) do update set info=array_append(tbl1.info, 100) returning *;
id | info
—-+———
1 | {1,2,3}
(1 row)
INSERT 0 1
postgres=# insert into tbl1 values (1, array[1,2,3]) on conflict (id) do update set info=array_append(tbl1.info, 100) returning *;
id | info
—-+————-
1 | {1,2,3,100}
(1 row)
INSERT 0 1
postgres=# insert into tbl1 values (1, null) on conflict (id) do update set info=array_append(tbl1.info, 100) returning *;
id | info
—-+—————–
1 | {1,2,3,100,100}
(1 row)
INSERT 0 1
掃描二維碼獲取更多訊息: