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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

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  
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
6月前
|
存储 NoSQL 关系型数据库
深入探索地理空间查询:如何优雅地在MySQL、PostgreSQL及Redis中实现精准的地理数据存储与检索技巧
深入探索地理空间查询:如何优雅地在MySQL、PostgreSQL及Redis中实现精准的地理数据存储与检索技巧
613 0
|
3月前
|
关系型数据库 分布式数据库 PolarDB
在PolarDB中,对于join操作,系统会采用拉取内表
【1月更文挑战第21天】【1月更文挑战第103篇】在PolarDB中,对于join操作,系统会采用拉取内表
19 1
|
5月前
|
安全 关系型数据库 分布式数据库
polardb大表修改字符集,怎么操作风险最低
polardb大表修改字符集,怎么操作风险最低
32 1
|
7月前
|
前端开发 关系型数据库 数据库
使用psql操作PostgreSQL数据库
使用psql操作PostgreSQL数据库
69 0
|
7月前
|
关系型数据库 MySQL 数据库
Python操作关系型数据库
Python是一种功能强大的编程语言,它有许多用于操作数据库的库。在本文中,我们将介绍如何使用Python连接、查询和操作关系型数据库(如MySQL、PostgreSQL和SQLite)。
85 3
|
7月前
|
存储 关系型数据库 数据库
沉浸式学习PostgreSQL|PolarDB 13: 博客、网站按标签内容检索, 并按匹配度排序
本文主要教大家怎么用好数据库, 而不是怎么运维管理数据库、怎么开发数据库内核.
696 0
|
9月前
|
关系型数据库 PostgreSQL
PostgreSQL事务提交日志与CLOG操作初步认识
PostgreSQL事务提交日志与CLOG操作初步认识
147 0
|
11月前
|
关系型数据库 MySQL 数据库
《阿里云认证的解析与实战-关系型数据库ACP认证》——RDS关系型数据库的解析与实践(下)操作演示—— 一、迁移数据库
《阿里云认证的解析与实战-关系型数据库ACP认证》——RDS关系型数据库的解析与实践(下)操作演示—— 一、迁移数据库
|
11月前
|
关系型数据库 RDS
《阿里云认证的解析与实战-关系型数据库ACP认证》——RDS关系型数据库的解析与实践(下)操作演示——二、数据库备份
《阿里云认证的解析与实战-关系型数据库ACP认证》——RDS关系型数据库的解析与实践(下)操作演示——二、数据库备份
|
11月前
|
关系型数据库 数据库 数据库管理
《阿里云认证的解析与实战-关系型数据库ACP认证》——RDS关系型数据库的解析与实践(下)操作演示——三、删除数据
《阿里云认证的解析与实战-关系型数据库ACP认证》——RDS关系型数据库的解析与实践(下)操作演示——三、删除数据

相关产品

  • 云原生数据库 PolarDB