PostgreSQL 实践 - 内容社区(如论坛)图式搜索应用

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

标签

PostgreSQL , 图数据库 , 论坛搜索 , 推荐 , 数组 , smlar相似搜索


背景

通常一个内容社区网站可能需要记录这么一些数据: 文章,用户,标签。

还有三者之间的关系,包括,文章的标签,用户阅读了文章,用户收藏了文章,用户关注了某用户,用户是某篇文章的作者。

最终要实现毫无人道的查询,例如:

阅读了此篇文章的人还在阅读什么其他文章,和我爱好相近的人都有哪些等等等等。

其中文章数量几千万,用户数量接近一千万。

如何实现这样的需求呢?

实际上PostgreSQL里面的数组、smlar实现这个需求非常的方便。下面开始设计和压测。

数组用于存储正向和反向关系,标签等。

smlar用于查询相似的数组(找出爱好相似的人)。

设计

元数据

1、用户表

create table users(  
  uid int primary key,             -- 用户ID  
  info text,           -- 附加信息  
  crt_time timestamp   -- 时间  
);  

2、标签表

create table tags(  
  tagid int primary key,              -- 标签ID  
  info text,              -- 附加信息   
  crt_time timestamp      -- 时间  
);  

3、文章表

create table arts(  
  artid int primary key,      -- 文章ID  
  info text,      -- 附加信息、内容  
  uids int[],     -- 用户IDs(作者可能是多人,所以使用数组)  
  tags int[]      -- 标签  
);  

关系数据

1、正向关系

1.1、文章被谁看过

create table art_uids_view (  
  artid int primary key,  
  uids int[]  
);  

1.2、文章被谁收藏过

create table art_uids_like (  
  artid int primary key,  
  uids int[]  
);  

2、反向关系

2.1、用户看过哪些文章,包含哪些标签

create table uid_arts_view (  
  uid int primary key,  
  arts int[],  
  tags int[]  
);  

2.2、用户收藏了哪些文章,包含哪些标签

create table uid_arts_like (  
  uid int primary key,  
  arts int[],  
  tags int[]  
);  

查询

1、阅读了此篇文章的其他人还在阅读什么其他文章,(过滤当前文章、以及我阅读过的文章)。

逻辑如下,写成UDF即可:

create extension intarray ;  -- 创建intarry插件,简化数组的加减  
  
select (uids - $current_uid) into v1 from art_uids_view where artid = $current_artid ;  -- 取出阅读了当前文章的所有用户(减去当前用户)  
  
select (unnest(arts) as arts, count(*) as cnt) into v2 from uid_arts_view where uid = any (v1) group by 1 ;  -- 获取阅读了同样文章的人,还阅读了哪些文章  
  
select arts into v3 from uid_arts_view where uid= $current_uid ;          -- 当前用户阅读了哪些文章  
  
result = v2.arts - v3 ;   -- 其他人阅读的所有文章 减去 当前用户阅读的文章,得到其他人阅读的文章。  按重叠数从大到小排序,推荐给用户   

UDF如下,都能使用索引,都是聚合后的点查,性能很赞:

create or replace function rec_arts_view(  
  i1 int,  -- 文章ID  
  i2 int,  -- 当前用户ID  
  i3 int   -- limit  
) returns setof int as $$  
declare  
  res int[];  -- 结果  
  v1 int[];   -- 文章被哪些用户阅读了  
begin  
    
  -- 文章被哪些用户阅读了  
  select (uids - i2) into v1 from art_uids_view where artid = i1 ;  
    
  -- 阅读了这篇文章的其他用户,阅读了哪些文章,排除当前用户已阅读的,按重复率排序,返回N条。  
  -- 如果阅读了该文章的其他人,还阅读了很多其他文章,排序可能会略耗时。
  return query  
  select t1.arts from   
  (  
    select unnest(arts) arts, count(*) cnt from uid_arts_view where uid = any (v1) group by 1  
  ) t1  
  left join  
  (  
    select unnest(arts) arts, 1 cnt from uid_arts_view where uid= i2   
  ) t2  
  on (t1.arts=t2.arts)   
  where t2.* is null  
  order by t1.cnt desc  
  limit i3;  
  
end;  
$$ language plpgsql strict;  

2、与我(阅读文章)爱好相近的人有哪些,走GIN索引,性能很赞。

create extension smlar;  
  
set smlar.type='overlap';        
set smlar.threshold=?;             -- 设置重叠阈值  
    
select arts into v1 from uid_arts_view where uid = ?;       -- 我阅读了哪些文章    
  
select      
    *,      
    smlar( arts, v1, 'N.i' )       -- 其他人与我阅读的文章的重叠数是多少  
  from      
    uid_arts_view      
  where      
    arts % v1                      -- where cosine similarity >= smlar.threshold      
;    

3、与我(阅读文章标签)爱好相近的人有哪些。

与2类似,略。

4、与我(收藏文章)爱好相近的人有哪些。

与2类似,略。

5、与我(收藏文章标签)爱好相近的人有哪些。

与2类似,略。

生成正反向关系的UDF

使用UDF,减少交互次数,完成以下几类业务逻辑的操作。UDF可以使用plpgsql编写,很简单,本文略:

https://www.postgresql.org/docs/10/static/plpgsql.html

1、新建文章的行为,自动产生标签,并更新或追加标签表。

insert into tags values ();  
  
insert into arts values ();  

2、阅读行为,修改正向反向关系。

文章的tags信息从arts里获取  
  
insert into art_uids_view values ();  
  
insert into uid_arts_view values ();  

3、收藏行为,修改正向反向关系。

文章的tags信息从arts里获取  
  
insert into art_uids_like values ();  
  
insert into uid_arts_like values ();  

索引

-- smlar 相似查询  
create index idx_gin_1 on art_uids_view using gin ( uids _int4_sml_ops );    
create index idx_gin_2 on art_uids_like using gin ( uids _int4_sml_ops );    
  
create index idx_gin_3 on uid_arts_view using gin ( arts _int4_sml_ops );    
create index idx_gin_4 on uid_arts_view using gin ( tags _int4_sml_ops );    
  
create index idx_gin_5 on uid_arts_like using gin ( arts _int4_sml_ops );    
create index idx_gin_6 on uid_arts_like using gin ( tags _int4_sml_ops );    
  
create index idx_gin_7 on art_uids_view using gin ( uids _int4_sml_ops );    
create index idx_gin_8 on art_uids_like using gin ( uids _int4_sml_ops );    

可选索引

-- 数组相交、包含查询  
create index idx_gin_01 on art_uids_view using gin ( uids gin__int_ops );    
create index idx_gin_02 on art_uids_like using gin ( uids gin__int_ops );    
  
create index idx_gin_03 on uid_arts_view using gin ( arts gin__int_ops );    
create index idx_gin_04 on uid_arts_view using gin ( tags gin__int_ops );    
  
create index idx_gin_05 on uid_arts_like using gin ( arts gin__int_ops );    
create index idx_gin_06 on uid_arts_like using gin ( tags gin__int_ops );    
  
create index idx_gin_07 on art_uids_view using gin ( uids gin__int_ops );    
create index idx_gin_08 on art_uids_like using gin ( uids gin__int_ops );    

填充测试数据

1、生成1000万用户

insert into users select id, md5(id::text), now() from generate_series(1,10000000) t(id);  

2、生成10万标签

insert into tags select id, md5(id::text), now() from generate_series(1,100000) t(id);  

3、生成5000万文章

create or replace function gen_arr(int,int) returns int[] as $$  
  select array(select ceil(random()*$1) from generate_series(1,$2))::int[];  
$$ language sql strict;  
insert into arts select id, md5(id::text),   
  gen_arr(10000000 ,3),    
  gen_arr(100000 ,10)   
from generate_series(1,50000000) t(id);  

4、生成正向关系,平均每篇文章被500人阅读,被50人收藏。

insert into art_uids_view select id, gen_arr(10000000, 500) from generate_series(1,50000000) t(id);  
  
insert into art_uids_like select id, gen_arr(10000000, 50) from generate_series(1,50000000) t(id);  

5、生成反向关系(按理说,反向关系和正向关系应该一一对应,为了测试方便,我这里就不对应了,测试效果是一样的)

平均每人阅读1000篇文章,涉及500个标签。收藏100篇文章,涉及50个标签。

insert into uid_arts_view select id, gen_arr(50000000, 1000), gen_arr(100000, 500) from generate_series(1,10000000) t(id);  
  
insert into uid_arts_like select id, gen_arr(50000000, 100), gen_arr(100000, 50) from generate_series(1,10000000) t(id);  

性能测试

1、阅读了此篇文章的其他人还在阅读什么其他文章,(过滤当前文章、以及我阅读过的文章)。

select rec_arts_view(1,2,10);    -- 文章ID=1, 当前用户ID=2,  返回10条推荐文章给当前用户。  

其他人一共阅读了约50万其他文章,获取加排序耗时:200毫秒。

postgres=# select count(*) from rec_arts_view(1,4,1000000);
 count  
--------
 497524
(1 row)

Time: 565.524 ms

postgres=# select count(*) from rec_arts_view(1,4,10);
 count 
-------
    10
(1 row)

Time: 198.368 ms

2、与我(阅读文章)爱好相近的人有哪些。

set smlar.type='overlap';        
set smlar.threshold=10;             -- 设置重叠阈值  
    
select arts into v1 from uid_arts_view where uid = 1;       -- 我阅读了哪些文章  
  
select      
    *,      
    smlar( arts, v1, 'N.i' )       -- 其他人与我阅读的文章的重叠数是多少  
  from      
    uid_arts_view      
  where      
    arts % v1                      -- where cosine similarity >= smlar.threshold      
;    

耗时:2.4毫秒。

 Bitmap Heap Scan on public.uid_arts_view  (cost=933.50..29296.93 rows=10000 width=72) (actual time=1.955..2.351 rows=2 loops=1)
   Output: uid, arts, tags, smlar(arts, '{25213311,49886221,45108456,27929256,20760231,35023889,17507871,43947072,48578113,41561690,39557908,26852171,29310889,5029778,24892381,12174141,9191797,41397570,25795440,27806324,28635176}'::integer[], 'N.i'::text)
   Recheck Cond: (uid_arts_view.arts % '{25213311,49886221,45108456,27929256,20760231,35023889,17507871,43947072,48578113,41561690,39557908,26852171,29310889,5029778,24892381,12174141,9191797,41397570,25795440,27806324,28635176}'::integer[])
   Heap Blocks: exact=2
   Buffers: shared hit=107
   ->  Bitmap Index Scan on idx_gin_3  (cost=0.00..931.00 rows=10000 width=0) (actual time=1.506..1.506 rows=2 loops=1)
         Index Cond: (uid_arts_view.arts % '{25213311,49886221,45108456,27929256,20760231,35023889,17507871,43947072,48578113,41561690,39557908,26852171,29310889,5029778,24892381,12174141,9191797,41397570,25795440,27806324,28635176}'::integer[])
         Buffers: shared hit=85
 Planning time: 0.110 ms
 Execution time: 2.378 ms
(10 rows)

预计算与性能优化

前面的推荐文章、找相似的人。指的是实时查询的性能,而实际这些操作都可以预计算的(因为文章增量不会太大、而且文章的阅读人群变化不会太大),例如一天刷新一次,那么像用户推荐相似用户,推荐相似文章时,有预计算则直接查询结果,那性能会提升到0.0N毫秒级响应。没有预计算的新文章,则实时查询(并更新到预计算的表中),也能够毫秒级响应。

预计算还可以做成另一种模式,当有人查询这篇文章时,根据上次预计算的时间,决定是否需要重新查询,并更新它。 (也就是说,实时计算 + 缓存 + 缓存超时 的模式。)

逻辑如下

select xxx from pre_view_tbl where xxx=xxx;  -- 查询缓存,return

-- 写入或更新缓存
if not found then
  -- 同步写入
  insert into pre_view_tbl select xxxx returning *; -- 实时计算, 并返回
else if mod_time < (now() - 超时阈值) then
  -- 异步
  delete from pre_view_tbl where xxx=xxx;
  insert into pre_view_tbl select xxxx; -- 实时计算
end if;

小结

3分开发,7分运营。内容网站与社交软件类似,运营是重头戏。运营中关键的一环是圈子,圈子可以聚人气,形成圈子往往靠的是推荐,推荐的源头又是行为,推荐什么样的内容、人给目标,靠的是行为。所谓物以类聚,人以群居,就是这个理。

PostgreSQL 的数组、smlar实现高效的归类查询、推荐需求非常的方便。

1、数组用于存储正向和反向关系,标签等。

2、smlar用于查询相似的数组(找出爱好相似的人)。

在社交运营、内容运营场景中,非常方便、高效。

热点人、热点文章也不在话下,在其他案例中已经测试过,可以参考本文末尾。

参考

https://www.postgresql.org/docs/10/static/plpgsql.html

《电商内容去重\内容筛选应用(实时识别转载\盗图\侵权?) - 文本、图片集、商品集、数组相似判定的优化和索引技术》

https://www.postgresql.org/docs/10/static/intarray.html

计数、实时需求也口可以使用流计算,案例参考:

《三体高可用PCC大赛 - facebook\微博 like场景 - 数据库设计与性能压测》

https://github.com/bitnine-oss/agensgraph

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3月前
|
存储 SQL Cloud Native
深入了解云原生数据库CockroachDB的概念与实践
作为一种全球领先的分布式SQL数据库,CockroachDB以其高可用性、强一致性和灵活性等特点备受关注。本文将深入探讨CockroachDB的概念、设计思想以及实践应用,并结合实例演示其在云原生环境下的优越表现。
|
3月前
|
Cloud Native 关系型数据库 大数据
CockroachDB:云原生数据库的新概念与实践
本文将介绍CockroachDB,一种先进的云原生数据库,它具备分布式、强一致性和高可用性等特点。我们将探讨CockroachDB的基本原理、架构设计以及在实际应用中的种种优势和挑战。
|
6月前
|
负载均衡 监控 关系型数据库
百度搜索:蓝易云【PostgreSQL 主从复制方案】
请注意,上述仅为一种主从复制方案的概述,实际实施时可能需要根据特定环境和需求进行调整。建议参考PostgreSQL官方文档和其他可靠资源获取更详细的指南和说明。
89 1
|
6月前
|
Ubuntu 关系型数据库 数据库
百度搜索:蓝易云【Ubuntu系统安装 PostgreSQL详细教程。】
现在,你已经成功在Ubuntu系统上安装了PostgreSQL,并创建了一个新的数据库和用户。你可以使用所创建的用户凭据连接到数据库并开始使用。记得根据你的具体需求进行进一步的配置和安全性调整。
251 2
|
7月前
|
关系型数据库 物联网 PostgreSQL
沉浸式学习PostgreSQL|PolarDB 11: 物联网(IoT)、监控系统、应用日志、用户行为记录等场景 - 时序数据高吞吐存取分析
物联网场景, 通常有大量的传感器(例如水质监控、气象监测、新能源汽车上的大量传感器)不断探测最新数据并上报到数据库. 监控系统, 通常也会有采集程序不断的读取被监控指标(例如CPU、网络数据包转发、磁盘的IOPS和BW占用情况、内存的使用率等等), 同时将监控数据上报到数据库. 应用日志、用户行为日志, 也就有同样的特征, 不断产生并上报到数据库. 以上数据具有时序特征, 对数据库的关键能力要求如下: 数据高速写入 高速按时间区间读取和分析, 目的是发现异常, 分析规律. 尽量节省存储空间
603 1
|
7月前
|
人工智能 关系型数据库 Serverless
阿里函数计算FC、文件存储NAS和RDS PostgreSQL的应用体验报告
本次体验的目的,旨在详细介绍如何通过阿里函数计算FC部署ChatGLM6B大语言模型,并借助文件存储NAS和RDS PostgreSQL搭建一个AI知识库问答应用,以实现PDF、TXT、HTML等文件和URL类型资料的轻松读取和处理。
243 62
|
4月前
|
SQL 关系型数据库 C语言
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
45 0
|
4月前
|
SQL 关系型数据库 数据库
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
49 0
|
4月前
|
关系型数据库 数据库 PostgreSQL
PostgreSQL【应用 01】使用Vector插件实现向量相似度查询(Docker部署的PostgreSQL安装pgvector插件说明)和Milvus向量库对比
PostgreSQL【应用 01】使用Vector插件实现向量相似度查询(Docker部署的PostgreSQL安装pgvector插件说明)和Milvus向量库对比
180 1
|
4月前
|
关系型数据库 数据库 PostgreSQL
Docker【应用 03】给Docker部署的PostgreSQL数据库安装PostGIS插件(安装流程及问题说明)
Docker【应用 03】给Docker部署的PostgreSQL数据库安装PostGIS插件(安装流程及问题说明)
149 0

相关产品

  • 云原生数据库 PolarDB