日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PostgreSQL 多重含义数组检索与条件过滤 (标签1:属性, 标签n:属性) - 包括UPSERT操作如何修改数组、追加数组元素

發布時間:2024/8/23 数据库 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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操作如何修改数组、追加数组元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。