PolarDB 开源基础教程系列 7.2 应用实践之 跨境电商场景

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 本文介绍了如何在跨境电商场景中快速判断商标或品牌侵权,避免因侵权带来的法律纠纷。通过创建品牌表并使用PostgreSQL的pg_trgm插件和GIN索引,实现了高性能的字符串相似匹配功能。与传统方法相比,PolarDB|PostgreSQL的方法不仅提升了上万倍的查询速度,还解决了传统方法难以处理的相似问题检索。具体实现步骤包括创建品牌表、插入随机品牌名、配置pg_trgm插件及索引,并设置相似度阈值进行高效查询。此外,文章还探讨了字符串相似度计算的原理及应用场景,提供了进一步优化和扩展的方向。

2、跨境电商场景, 快速判断商标|品牌侵权

b站视频链接

Youtube视频链接


2、跨境电商场景, 快速判断商标|品牌侵权

很多业务场景中需要判断商标侵权, 避免纠纷. 例如

  • 电商的商品文字描述、图片描述中可能有侵权内容. 特别是跨境电商, 在一些国家侵权查处非常严厉.
  • 注册公司名、产品名时可能侵权.
  • 在写文章时, 文章的文字内容、视频内容、图片内容中的描述可能侵权.

而且商标侵权通常还有相似的情况, 例如修改大品牌名字的其中的个别字母, 蹭大品牌的流量, 导致大品牌名誉受损.

例如postgresql是个商标, 如果你使用posthellogresql、postgresqlabc, p0stgresql也可能算侵权.

以跨境电商为力, 为了避免侵权, 在发布内容时需要商品描述中出现的品牌名、产品名等是否与已有的商标库有相似.

对于跨境电商场景, 由于店铺和用户众多, 商品的修改、发布是比较高频的操作, 所以需要实现高性能的字符串相似匹配功能.

一、准备数据

创建一张品牌表, 用于存储收集好的注册商标(通常最终转换为文字).

create unlogged table tbl_ip (   -- 测试使用unlogged table, 加速数据生成    
  id serial primary key,  -- 每一条品牌信息的唯一ID    
  n text  -- 品牌名    
);

使用随机字符模拟生成1000万条品牌名.

insert into tbl_ip (n) select md5(random()::text) from generate_series(1,10000000);

再放入几条比较容易识别的:

insert into tbl_ip(n) values ('polardb'),('polardbpg'),('polardbx'),('alibaba'),('postgresql'),('mysql'),('aliyun'),('apsaradb'),('apple'),('microsoft');
postgres=# select * from tbl_ip limit 10;    
 id |                n                     
----+----------------------------------    
  1 | f4cd4669d249c1747c1d31b0b492d84e    
  2 | 2e29f32460485698088f4ab0632d86b7    
  3 | a8460622db4a3dc4ab70a8443a2c2a1a    
  4 | c4554856e259d3dfcccfb3c9872ab1d0    
  5 | b3a6041c5838d70d95a1316eea45bea3    
  6 | fc2d701eca05c74905fd1a604f072006    
  7 | f3dc443060e33bb672dc6a3b79bc1acd    
  8 | 1305b6092f9e798453e9f60840b8db2a    
  9 | 9b07cad251661627e15f239e5b122eaf    
 10 | 8b5d2a468435febe417b17d0d0442b86    
(10 rows)    
    
postgres=# select count(*) from tbl_ip;    
  count       
----------    
 10000010    
(1 row)

二、传统方法只能使用like全模糊查询, 但是局部侵权的可能性非常多, 使用模糊查询需要很多很多组合, 性能会非常差.

例如postgresql是个商标, 如果用户使用了一个字符串为以下组合, 都可能算侵权:

  • post
  • postgres
  • sql
  • gresql
  • postgresql
  • postgre

写成SQL应该是这样的

select * from tbl_ip where    
  n like '%post%' or    
  n like '%postgres%' or    
  n like '%sql%' or    
  n like '%gresql%' or    
  n like '%postgresql%' or    
  n like '%postgre%';

结果

id    |     n          
----------+------------    
 10000005 | postgresql    
 10000006 | mysql    
(2 rows)

耗时如下

QUERY PLAN                                                                                      
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    
 Seq Scan on tbl_ip  (cost=0.00..333336.00 rows=5999 width=37) (actual time=2622.461..2622.463 rows=2 loops=1)    
   Filter: ((n ~~ '%post%'::text) OR (n ~~ '%postgres%'::text) OR (n ~~ '%sql%'::text) OR (n ~~ '%gresql%'::text) OR (n ~~ '%postgresql%'::text) OR (n ~~ '%postgre%'::text))    
   Rows Removed by Filter: 10000008    
 Planning Time: 1.381 ms    
 JIT:    
   Functions: 2    
   Options: Inlining false, Optimization false, Expressions true, Deforming true    
   Timing: Generation 1.442 ms, Inlining 0.000 ms, Optimization 1.561 ms, Emission 6.486 ms, Total 9.489 ms    
 Execution Time: 2624.001 ms    
(9 rows)

三、基于 PolarDB|PostgreSQL 特性的设计和实验

使用pg_trgm插件, gin索引, 以及它的字符串相似查询功能,

创建插件

postgres=# create extension if not exists pg_trgm;    
NOTICE:  extension "pg_trgm" already exists, skipping    
CREATE EXTENSION

创建索引

postgres=# create index on tbl_ip using gin (n gin_trgm_ops);

设置相似度阈值, 仅返回相似度大于0.9的记录

postgres=# set pg_trgm.similarity_threshold=0.9;    
SET

使用相似度查询

select *,     
  similarity(n, 'post'),    
  similarity(n, 'postgres'),    
  similarity(n, 'sql'),    
  similarity(n, 'gresql'),    
  similarity(n, 'postgresql'),    
  similarity(n, 'postgre')    
from tbl_ip     
where    
  n % 'post' or    
  n % 'postgres' or    
  n % 'sql' or    
  n % 'gresql' or    
  n % 'postgresql' or    
  n % 'postgre';

结果

id    |     n      | similarity | similarity | similarity | similarity | similarity | similarity     
----------+------------+------------+------------+------------+------------+------------+------------    
 10000005 | postgresql | 0.33333334 |  0.6666667 | 0.15384616 |  0.3846154 |          1 |  0.5833333    
(1 row)

耗时如下

QUERY PLAN                                                                                
------------------------------------------------------------------------------------------------------------------------------------------------------------------    
 Bitmap Heap Scan on tbl_ip  (cost=996.70..7365.20 rows=5999 width=37) (actual time=0.180..0.183 rows=1 loops=1)    
   Recheck Cond: ((n % 'post'::text) OR (n % 'postgres'::text) OR (n % 'sql'::text) OR (n % 'gresql'::text) OR (n % 'postgresql'::text) OR (n % 'postgre'::text))    
   Heap Blocks: exact=1    
   ->  BitmapOr  (cost=996.70..996.70 rows=6000 width=0) (actual time=0.140..0.141 rows=0 loops=1)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..115.30 rows=1000 width=0) (actual time=0.053..0.053 rows=0 loops=1)    
               Index Cond: (n % 'post'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..200.00 rows=1000 width=0) (actual time=0.019..0.019 rows=0 loops=1)    
               Index Cond: (n % 'postgres'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..93.30 rows=1000 width=0) (actual time=0.007..0.007 rows=0 loops=1)    
               Index Cond: (n % 'sql'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..157.10 rows=1000 width=0) (actual time=0.011..0.011 rows=0 loops=1)    
               Index Cond: (n % 'gresql'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..242.90 rows=1000 width=0) (actual time=0.035..0.035 rows=1 loops=1)    
               Index Cond: (n % 'postgresql'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..179.10 rows=1000 width=0) (actual time=0.013..0.013 rows=0 loops=1)    
               Index Cond: (n % 'postgre'::text)    
 Planning Time: 4.682 ms    
 Execution Time: 0.272 ms    
(18 rows)

使用了pg_trgm后, 即使是like查询响应速度也飞快:

postgres=# explain analyze select * from tbl_ip where    
  n like '%post%' or    
  n like '%postgres%' or    
  n like '%sql%' or    
  n like '%gresql%' or    
  n like '%postgresql%' or    
  n like '%postgre%';    
                                                                                     QUERY PLAN                                                                                         
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    
 Bitmap Heap Scan on tbl_ip  (cost=612.80..6981.30 rows=5999 width=37) (actual time=0.122..0.126 rows=2 loops=1)    
   Recheck Cond: ((n ~~ '%post%'::text) OR (n ~~ '%postgres%'::text) OR (n ~~ '%sql%'::text) OR (n ~~ '%gresql%'::text) OR (n ~~ '%postgresql%'::text) OR (n ~~ '%postgre%'::text))    
   Heap Blocks: exact=1    
   ->  BitmapOr  (cost=612.80..612.80 rows=6000 width=0) (actual time=0.099..0.101 rows=0 loops=1)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..50.40 rows=1000 width=0) (actual time=0.047..0.048 rows=1 loops=1)    
               Index Cond: (n ~~ '%post%'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..136.20 rows=1000 width=0) (actual time=0.011..0.011 rows=1 loops=1)    
               Index Cond: (n ~~ '%postgres%'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..29.50 rows=1000 width=0) (actual time=0.003..0.003 rows=2 loops=1)    
               Index Cond: (n ~~ '%sql%'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..93.30 rows=1000 width=0) (actual time=0.014..0.014 rows=1 loops=1)    
               Index Cond: (n ~~ '%gresql%'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..179.10 rows=1000 width=0) (actual time=0.014..0.014 rows=1 loops=1)    
               Index Cond: (n ~~ '%postgresql%'::text)    
         ->  Bitmap Index Scan on tbl_ip_n_idx  (cost=0.00..115.30 rows=1000 width=0) (actual time=0.008..0.008 rows=1 loops=1)    
               Index Cond: (n ~~ '%postgre%'::text)    
 Planning Time: 0.571 ms    
 Execution Time: 0.207 ms    
(18 rows)

四、传统方法与PolarDB|PostgreSQL的对照

品牌数 传统like查询耗时 ms PolarDB|PostgreSQL pg_trgm近似查询耗时 ms PolarDB|PostgreSQL pg_trgm like查询耗时 ms
1000万条 2624.001 0.272 0.207

毫无疑问, PolarDB|PostgreSQL性能提升了上万倍, 而且解决了传统方法无法解决的相似问题检索.

五、知识点

1、pg_trgm

https://www.postgresql.org/docs/16/pgtrgm.html

如何计算两个字符串的相似度:

  • 1、切词. 非字母或数字都被认为是word分隔符, 将字符串拆分成若干个word.
  • 2、将word转换成token. 在每个word的前面加2个空格, 每个word的末尾加1个空格, 然后以连续的三个字符为一组, 从头开始切, 将每个" word "切分为若干个“3个字符的token”.
  • 3、去除重复token, 得到一组token.
  • 4、根据token来计算2个字符串的相似性. 注意有不同的算法.

将字符串转换生成token的例子:

-- 第一步得到two和words, 然后得到"  two "和"  words ", 然后得到以下.     
postgres=# select show_trgm('two ,words');      
                       show_trgm                           
-------------------------------------------------------    
 {"  t","  w"," tw"," wo","ds ",ord,rds,two,"wo ",wor}    
(1 row)    
    
postgres=# select show_trgm('two , words');    
                       show_trgm                           
-------------------------------------------------------    
 {"  t","  w"," tw"," wo","ds ",ord,rds,two,"wo ",wor}    
(1 row)    
    
postgres=# select show_trgm(' two , words   ');    
                       show_trgm                           
-------------------------------------------------------    
 {"  t","  w"," tw"," wo","ds ",ord,rds,two,"wo ",wor}    
(1 row)    
    
-- 结果token会去重      
postgres=# select show_trgm('two two1');       
             show_trgm                 
-----------------------------------    
 {"  t"," tw","o1 ",two,"wo ",wo1}    
(1 row)    
    
postgres=# select show_trgm('two');    
        show_trgm            
-------------------------    
 {"  t"," tw",two,"wo "}    
(1 row)    
    
postgres=# select show_trgm('words');    
            show_trgm                
---------------------------------    
 {"  w"," wo","ds ",ord,rds,wor}    
(1 row)    
    
postgres=# select show_trgm('abc');    
        show_trgm            
-------------------------    
 {"  a"," ab",abc,"bc "}    
(1 row)    
    
postgres=# select show_trgm('abc hello');    
                       show_trgm                           
-------------------------------------------------------    
 {"  a","  h"," ab"," he",abc,"bc ",ell,hel,llo,"lo "}    
(1 row)

比较两个字符串相似性的算法: 详见 contrib/pg_trgm/trgm_op.c

1: similarity (%) (t % 'word'  ==> 计算相似性对应  similarity(t, 'word'))

相似性 = 两个字符串的token交集去重后的个数 / 两个字符串的token并集去重后的个数

大致可以表达: 两个字符串的整体相似性.

阈值参数: pg_trgm.similarity_threshold (real)

2: word_similarity (<% and %>) ('word' <% t  ==> 计算相似性对应  word_similarity('word', t))

word_similarity(string1, string2) == count.匹配string1 token的(token(substring(string2中的任意连续的word组))) / count(token(string1))

大致可以表达: 字符串2的若干连续字符与字符串1的相似度.

阈值参数: pg_trgm.word_similarity_threshold (real)

3: strict_word_similarity (<<% and %>>) ('word' <<% t  ==> 计算相似性对应  strict_word_similarity('word', t))

strict_word_similarity(string1, string2) == max( similarity(string1, string2中的任意连续的word组) )

大致可以表达: 字符串2的若干连续单词与字符串1的相似度.

相似度阈值参数, 相似度大于阈值时, 对应的相似操作符返回true的结果.

阈值参数: pg_trgm.strict_word_similarity_threshold (real)

计算两个字符串相似度的例子:

postgres=# select similarity('abc','abc hello');    
 similarity     
------------    
        0.4    
(1 row)    
postgres=# select similarity('abc hello','abc');    
 similarity     
------------    
        0.4    
(1 row)    
    
word_similarity    
    
    
postgres=# select word_similarity('abc','abc hello');    
 word_similarity     
-----------------    
               1    
(1 row)    
    
postgres=# select word_similarity('abc hello','abc');    
 word_similarity     
-----------------    
             0.4    
(1 row)    
    
strict_word_similarity    
    
    
postgres=# select strict_word_similarity('abc','abc hello');    
 strict_word_similarity     
------------------------    
                      1    
(1 row)    
    
    
postgres=# select strict_word_similarity('abc hello','abc');    
 strict_word_similarity     
------------------------    
                    0.4    
(1 row)    
    
    
postgres=# select similarity('word', 'wor ord');    
 similarity     
------------    
      0.625    
(1 row)    
    
postgres=# select similarity('word', 'ord wor');    
 similarity     
------------    
      0.625    
(1 row)    
    
postgres=# select word_similarity('word', 'ord wor');    
 word_similarity     
-----------------    
               1    
(1 row)    
    
postgres=# select word_similarity('word', 'wor ord');    
 word_similarity     
-----------------    
           0.625    
(1 row)    
    
postgres=# select strict_word_similarity('word', 'wor ord');    
 strict_word_similarity     
------------------------    
                  0.625    
(1 row)    
    
postgres=# select strict_word_similarity('word', 'ord wor');    
 strict_word_similarity     
------------------------    
                  0.625    
(1 row)

六、思考

为什么传统方法与pg_trgm相比性能相差这么大?

字符串近似查询还可以应用于哪些场景?

前后模糊查询优化方法?

如果将相似度调低, 性能还能这么好吗?

如果想返回最相似的一条, 怎么优化查询效果最佳? 《PostgreSQL 相似搜索设计与性能 - 地址、QA、POI等文本 毫秒级相似搜索实践》

和smlar插件相比, 搜索算法是否有相似之处?

还有哪些相似算法? 《PolarDB 开源版通过pg_similarity实现17种文本相似搜索 - token归一切分, 根据文本相似度检索相似文本.》

语义/情感相似怎么实现? vector

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
27天前
|
存储 NoSQL 关系型数据库
PolarDB开源数据库进阶课17 集成数据湖功能
本文介绍了如何在PolarDB数据库中接入pg_duckdb、pg_mooncake插件以支持数据湖功能, 可以读写对象存储的远程数据, 支持csv, parquet等格式, 支持delta等框架, 并显著提升OLAP性能。
55 1
|
20天前
|
关系型数据库 分布式数据库 数据库
喜报|PolarDB开源社区荣获“2024数据库国内活跃开源项目”奖
喜报|PolarDB开源社区荣获“2024数据库国内活跃开源项目”奖
|
27天前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课18 通过pg_bulkload适配pfs实现批量导入提速
本文介绍了如何修改 `pg_bulkload` 工具以适配 PolarDB 的 PFS(Polar File System),从而加速批量导入数据。实验环境依赖于 Docker 容器中的 loop 设备模拟共享存储。通过对 `writer_direct.c` 文件的修改,替换了一些标准文件操作接口为 PFS 对应接口,实现了对 PolarDB 15 版本的支持。测试结果显示,使用 `pg_bulkload` 导入 1000 万条数据的速度是 COPY 命令的三倍多。此外,文章还提供了详细的步骤和代码示例,帮助读者理解和实践这一过程。
43 0
|
27天前
|
存储 容灾 关系型数据库
PolarDB开源数据库进阶课11 激活容灾(Standby)节点
本文介绍了如何激活PolarDB容灾(Standby)节点,实验环境依赖于Docker容器中用loop设备模拟共享存储。通过`pg_ctl promote`命令可以将Standby节点提升为主节点,使其能够接收读写请求。激活后,原Standby节点不能再成为PolarDB集群的Standby节点。建议删除对应的复制槽位以避免WAL文件堆积。相关操作和配置请参考系列文章及视频教程。
35 1
|
27天前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课5 在线备份
本文介绍了如何在PolarDB RAC一写多读集群中进行在线备份,特别针对共享存储模式。通过使用`polar_basebackup`工具,可以将实例的本地数据和共享数据备份到本地盘中。实验环境依赖于Docker容器中用loop设备模拟的共享存储。
33 1
|
27天前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课15 集成DeepSeek等大模型
本文介绍了如何在PolarDB数据库中接入私有化大模型服务,以实现多种应用场景。实验环境依赖于Docker容器中的loop设备模拟共享存储,具体搭建方法可参考相关系列文章。文中详细描述了部署ollama服务、编译并安装http和openai插件的过程,并通过示例展示了如何使用这些插件调用大模型API进行文本分析和情感分类等任务。此外,还探讨了如何设计表结构及触发器函数自动处理客户反馈数据,以及生成满足需求的SQL查询语句。最后对比了不同模型的回答效果,展示了deepseek-r1模型的优势。
91 0
|
27天前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课14 纯享单机版
PolarDB不仅支持基于“共享存储+多计算节点”的集群版,还提供类似开源PostgreSQL的单机版。单机版部署简单,适合大多数应用场景,并可直接使用PostgreSQL生态插件。通过Docker容器、Git克隆代码、编译软件等步骤,即可完成PolarDB单机版的安装与配置。具体操作包括启动容器、进入容器、克隆代码、编译软件、初始化实例、配置参数及启动数据库。此外,还有多个相关教程和视频链接供参考,帮助用户更好地理解和使用PolarDB单机版。
37 0
|
27天前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课13 单机版转换为集群版
本文介绍如何将“本地存储实例”转换为“共享存储实例”,依赖于先前搭建的实验环境。主要步骤包括:准备PFS二进制文件、格式化共享盘为pfs文件系统、启动pfsd服务、停库并拷贝数据到pfs内、修改配置文件,最后启动实例。通过这些操作,成功实现了从本地存储到共享存储的转换,并验证了新实例的功能。相关系列文章和视频链接提供了更多背景信息和技术细节。
21 0
|
27天前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课7 实时流式归档
本文介绍了如何在PolarDB RAC一写多读集群中实现实时归档,确保WAL日志的及时备份。实验依赖于Docker容器和loop设备模拟的共享存储环境。通过配置主节点的`pg_hba.conf`、创建复制槽以及使用`pg_receivewal`工具,实现实时接收并归档WAL文件。此外,还提供了详细的命令行帮助和相关文档链接,方便读者参考和操作。注意:如果已搭建容灾节点,则无需重复进行实时归档。
12 0
|
21天前
|
Cloud Native 关系型数据库 分布式数据库
世界第一!阿里云PolarDB刷新全球数据库性能及性价比记录
世界第一!阿里云PolarDB刷新全球数据库性能及性价比记录

相关产品

  • 云原生数据库 PolarDB