PostgreSQL 多重含义数组检索与条件过滤 (标签1:属性, 标签n:属性) - 包括UPSERT操作如何修改数组、追加数组元素
摘要: 標簽 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
select?array(select?substring(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
select?substring($1[array_position(get_label($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功能是不是很贊呢?
UPSERT時,如何修改數組、追加數組元素?
https://www.postgresql.org/docs/10/static/functions-array.html
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
掃描二維碼獲取更多消息:
總結
以上是生活随笔為你收集整理的PostgreSQL 多重含义数组检索与条件过滤 (标签1:属性, 标签n:属性) - 包括UPSERT操作如何修改数组、追加数组元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Logtail从入门到精通(二):开启日
- 下一篇: 复杂 SQL 查询跑不动?DRDS 只读