如何用 PolarDB 整合age算法插件, 实现图式搜索加速 - 刑侦、社交、风控、族谱、推荐等业务图谱类关系数据搜索

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 背景PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.本文将介绍PolarDB结合图式算法, 实现高效率的刑侦、社交、风控、族谱、推荐等业...

背景

PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.

本文将介绍PolarDB结合图式算法, 实现高效率的刑侦、社交、风控、族谱、推荐等业务图谱类关系数据搜索.

age是什么

https://age.apache.org/age-manual/master/intro/overview.html

Apache AGE is a PostgreSQL extension that provides graph database functionality. AGE is an acronym for A Graph Extension, and is inspired by Bitnine’s fork of PostgreSQL 10, AgensGraph, which is a multi-model database. The goal of the project is to create single storage that can handle both relational and graph model data so that users can use standard ANSI SQL along with openCypher, the Graph query language.

简单来说就是一个支持图式数据和搜索的多模数据库插件.

将age整合到PolarDB

https://age.apache.org/age-manual/master/intro/setup.html

https://age.apache.org/download

https://github.com/apache/age

https://github.com/apache/age/tree/release/1.1.0

  1. 下载最新分支并安装

git clone --branch release/1.1.0 --depth 1 https://github.com/apache/age  
  
cd age  
  
git branch  
* release/1.1.0  
  
which pg_config  
~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config  
  1. 修复代码错误, 原因是RTE解析未兼容.

USE_PGXS=1 PG_CONFIG=~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config make  
  
报错如下:  
src/backend/parser/cypher_analyze.c: In function ‘convert_cypher_walker’:  
src/backend/parser/cypher_analyze.c:178:17: error: ‘QTW_EXAMINE_RTES’ undeclared (first use in this function); did you mean ‘QTW_EXAMINE_RTES_AFTER’?  
  178 |         flags = QTW_EXAMINE_RTES | QTW_IGNORE_RT_SUBQUERIES |  
      |                 ^~~~~~~~~~~~~~~~  
      |                 QTW_EXAMINE_RTES_AFTER  
src/backend/parser/cypher_analyze.c:178:17: note: each undeclared identifier is reported only once for each function it appears in  
make: *** [<builtin>: src/backend/parser/cypher_analyze.o] Error 1  

原因如下:

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=18c0da88a5d9da566c3bfac444366b73bd0b57da

Split QTW_EXAMINE_RTES flag into QTW_EXAMINE_RTES_BEFORE/_AFTER.  
  
This change allows callers of query_tree_walker() to choose whether  
to visit an RTE before or after visiting the contents of the RTE  
(i.e., prefix or postfix tree order).  All existing users of  
QTW_EXAMINE_RTES want the QTW_EXAMINE_RTES_BEFORE behavior, but  
an upcoming patch will want QTW_EXAMINE_RTES_AFTER, and it seems  
like a potentially useful change on its own.  
  
Andreas Karlsson (extracted from CTE inlining patch)  
  
Discussion: https://postgr.es/m/8810.1542402910@sss.pgh.pa.us  

https://git.postgresql.org/gitweb/?p=postgresql.git;a=blobdiff;f=src/include/nodes/nodeFuncs.h;h=a9f76bbb330a3a271363be317fd8caea3e09fe7d;hp=7739600db26e55628778d93d1e2a3833d90954d9;hb=18c0da88a5d9da566c3bfac444366b73bd0b57da;hpb=ff750ce2d82979e9588c629955e161a9379b05f3

-#define QTW_EXAMINE_RTES           0x10    /* examine RTEs */  
-#define QTW_DONT_COPY_QUERY            0x20    /* do not copy top Query */  
+#define QTW_EXAMINE_RTES_BEFORE        0x10    /* examine RTE nodes before their  
+                                            * contents */  
+#define QTW_EXAMINE_RTES_AFTER     0x20    /* examine RTE nodes after their  
+                                            * contents */  
+#define QTW_DONT_COPY_QUERY            0x40    /* do not copy top Query */  

修复如下:

cd age  
vi src/backend/parser/cypher_analyze.c  
  
  
        /*  
         * QTW_EXAMINE_RTES  
         *     We convert RTE_FUNCTION (cypher()) to RTE_SUBQUERY (SELECT)  
         *     in-place.  
         *  
         * QTW_IGNORE_RT_SUBQUERIES  
         *     After the conversion, we don't need to traverse the resulting  
         *     RTE_SUBQUERY. However, we need to traverse other RTE_SUBQUERYs.  
         *     This is done manually by the RTE_SUBQUERY case above.  
         *  
         * QTW_IGNORE_JOINALIASES  
         *     We are not interested in this.  
         */  
        // flags = QTW_EXAMINE_RTES | QTW_IGNORE_RT_SUBQUERIES |  
        flags = QTW_EXAMINE_RTES_BEFORE | QTW_IGNORE_RT_SUBQUERIES |  
                QTW_IGNORE_JOINALIASES;   

以上参考12分支:

https://github.com/apache/age/blob/release/PG12/1.1.0/src/backend/parser/cypher_analyze.c

        /*  
         * QTW_EXAMINE_RTES  
         *     We convert RTE_FUNCTION (cypher()) to RTE_SUBQUERY (SELECT)  
         *     in-place.  
         *  
         * QTW_IGNORE_RT_SUBQUERIES  
         *     After the conversion, we don't need to traverse the resulting  
         *     RTE_SUBQUERY. However, we need to traverse other RTE_SUBQUERYs.  
         *     This is done manually by the RTE_SUBQUERY case above.  
         *  
         * QTW_IGNORE_JOINALIASES  
         *     We are not interested in this.  
         */  
        flags = QTW_EXAMINE_RTES_BEFORE | QTW_IGNORE_RT_SUBQUERIES |  
                QTW_IGNORE_JOINALIASES;  

修复后继续安装即可

USE_PGXS=1 PG_CONFIG=~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config make  
USE_PGXS=1 PG_CONFIG=~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config make install  
  1. 使用age

postgres=# LOAD 'age';  
LOAD  
postgres=# SET search_path = ag_catalog, "$user", public;  
SET  
  
-- 以上也可以根据需要配置到数据库参数postgresql.conf 中, 即自动加载age:   
-- #shared_preload_libraries = ''  # (change requires restart)  
-- #local_preload_libraries = ''  
-- #session_preload_libraries = ''  
-- #search_path = '"$user", public'        # schema names  
  
postgres=# create extension age ;  
CREATE EXTENSION  
  1. 一些图式查询语法例子

postgres=# SELECT * FROM ag_catalog.create_graph('graph_name');  
NOTICE:  graph "graph_name" has been created  
 create_graph   
--------------  
   
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$#     RETURN 1  
postgres$# $$) AS (int_result agtype);  
 int_result   
------------  
 1  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$#     WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst  
postgres$#     RETURN lst  
postgres$# $$) AS (lst agtype);  
                lst                   
------------------------------------  
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$#     WITH {listKey: [{inner: 'Map1'}, {inner: 'Map2'}], mapKey: {i: 0}} as m  
postgres$#     RETURN m.listKey[0]  
postgres$# $$) AS (m agtype);  
         m           
-------------------  
 {"inner": "Map1"}  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# WITH {id: 0, label: "label_name", properties: {i: 0}}::vertex as v  
postgres$# RETURN v  
postgres$# $$) AS (v agtype);  
                                v                                   
------------------------------------------------------------------  
 {"id": 0, "label": "label_name", "properties": {"i": 0}}::vertex  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# WITH {id: 2, start_id: 0, end_id: 1, label: "label_name", properties: {i: 0}}::edge as e  
postgres$# RETURN e  
postgres$# $$) AS (e agtype);  
                                             e                                                
--------------------------------------------------------------------------------------------  
 {"id": 2, "label": "label_name", "end_id": 1, "start_id": 0, "properties": {"i": 0}}::edge  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# WITH [{id: 0, label: "label_name_1", properties: {i: 0}}::vertex,  
postgres$#             {id: 2, start_id: 0, end_id: 1, label: "edge_label", properties: {i: 0}}::edge,  
postgres$#            {id: 1, label: "label_name_2", properties: {}}::vertex  
postgres$#            ]::path as p  
postgres$# RETURN p  
postgres$# $$) AS (p agtype);  
                                                                                                                  p                                                                                         
                              
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
----------------------------  
 [{"id": 0, "label": "label_name_1", "properties": {"i": 0}}::vertex, {"id": 2, "label": "edge_label", "end_id": 1, "start_id": 0, "properties": {"i": 0}}::edge, {"id": 1, "label": "label_name_2", "prop  
erties": {}}::vertex]::path  
(1 row)  
  
postgres=# WITH graph_query as (  
postgres(#     SELECT *  
postgres(#         FROM cypher('graph_name', $$  
postgres$#         MATCH (n)  
postgres$#         RETURN n.name, n.age  
postgres$#     $$) as (name agtype, age agtype)  
postgres(# )  
postgres-# SELECT * FROM graph_query;  
 name | age   
------+-----  
(0 rows)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# MATCH (n)  
postgres$# RETURN n.name  
postgres$# ORDER BY n.name  
postgres$# SKIP 3  
postgres$# $$) as (names agtype);  
 names   
-------  
(0 rows)  

更多用法请参考age文档:

https://age.apache.org/age-manual/master/intro/overview.html

参考

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
28天前
|
机器学习/深度学习 算法 前端开发
别再用均值填充了!MICE算法教你正确处理缺失数据
MICE是一种基于迭代链式方程的缺失值插补方法,通过构建后验分布并生成多个完整数据集,有效量化不确定性。相比简单填补,MICE利用变量间复杂关系,提升插补准确性,适用于多变量关联、缺失率高的场景。本文结合PMM与线性回归,详解其机制并对比效果,验证其在统计推断中的优势。
603 11
别再用均值填充了!MICE算法教你正确处理缺失数据
|
2月前
|
机器学习/深度学习 算法 安全
【无人机三维路径规划】基于非支配排序的鲸鱼优化算法NSWOA与多目标螳螂搜索算法MOMSA求解无人机三维路径规划研究(Matlab代码实现)
【无人机三维路径规划】基于非支配排序的鲸鱼优化算法NSWOA与多目标螳螂搜索算法MOMSA求解无人机三维路径规划研究(Matlab代码实现)
118 5
|
2月前
|
传感器 机器学习/深度学习 算法
【使用 DSP 滤波器加速速度和位移】使用信号处理算法过滤加速度数据并将其转换为速度和位移研究(Matlab代码实现)
【使用 DSP 滤波器加速速度和位移】使用信号处理算法过滤加速度数据并将其转换为速度和位移研究(Matlab代码实现)
179 1
|
2月前
|
机器学习/深度学习 算法 安全
【无人机三维路径规划】多目标螳螂搜索算法MOMSA与非支配排序的鲸鱼优化算法NSWOA求解无人机三维路径规划研究(Matlab代码实现)
【无人机三维路径规划】多目标螳螂搜索算法MOMSA与非支配排序的鲸鱼优化算法NSWOA求解无人机三维路径规划研究(Matlab代码实现)
123 0
|
2月前
|
机器学习/深度学习 算法 调度
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
277 0
|
20天前
|
算法 数据可视化 测试技术
HNSW算法实战:用分层图索引替换k-NN暴力搜索
HNSW是一种高效向量检索算法,通过分层图结构实现近似最近邻的对数时间搜索,显著降低查询延迟。相比暴力搜索,它在保持高召回率的同时,将性能提升数十倍,广泛应用于大规模RAG系统。
101 10
HNSW算法实战:用分层图索引替换k-NN暴力搜索
|
20天前
|
机器学习/深度学习 人工智能 算法
【基于TTNRBO优化DBN回归预测】基于瞬态三角牛顿-拉夫逊优化算法(TTNRBO)优化深度信念网络(DBN)数据回归预测研究(Matlab代码实现)
【基于TTNRBO优化DBN回归预测】基于瞬态三角牛顿-拉夫逊优化算法(TTNRBO)优化深度信念网络(DBN)数据回归预测研究(Matlab代码实现)
|
2月前
|
存储 监控 算法
企业电脑监控系统中基于 Go 语言的跳表结构设备数据索引算法研究
本文介绍基于Go语言的跳表算法在企业电脑监控系统中的应用,通过多层索引结构将数据查询、插入、删除操作优化至O(log n),显著提升海量设备数据管理效率,解决传统链表查询延迟问题,实现高效设备状态定位与异常筛选。
95 3
|
3月前
|
机器学习/深度学习 算法 文件存储
神经架构搜索NAS详解:三种核心算法原理与Python实战代码
神经架构搜索(NAS)正被广泛应用于大模型及语言/视觉模型设计,如LangVision-LoRA-NAS、Jet-Nemotron等。本文回顾NAS核心技术,解析其自动化设计原理,探讨强化学习、进化算法与梯度方法的应用与差异,揭示NAS在大模型时代的潜力与挑战。
672 6
神经架构搜索NAS详解:三种核心算法原理与Python实战代码
|
2月前
|
存储 算法 数据可视化
基于禁忌搜索算法的TSP问题最优路径搜索matlab仿真
本程序基于禁忌搜索算法解决旅行商问题(TSP),旨在寻找访问多个城市的最短路径。使用 MATLAB 2022A 编写,包含城市坐标生成、路径优化及结果可视化功能。通过禁忌列表、禁忌长度与藐视准则等机制,提升搜索效率与解的质量,适用于物流配送、路径规划等场景。

热门文章

最新文章