本文主要介绍“PostgreSQL有哪些类似的搜索插件”。在日常操作中,相信很多人对PostgreSQL类似的搜索插件有什么疑惑。边肖查阅了各种资料,整理出简单易用的操作方法,希望能帮助大家解答“PostgreSQL有哪些类似的搜索插件”的疑惑!接下来,请和边肖一起学习!
类别1 : 元素重叠度相似
基于元素重叠度的相似度计算。广泛用于数组的相似搜索、全文搜索、字符串、文本特征值和多列任意组合查询。
PostgreSQL插件表示如下
1、rum
https://github.com/postgrespro/rum
2、pg_trgm
https://www.postgresql.org/docs/devel/static/pgtrgm.html
3、smlar
http://sigaev.ru/git/gitweb.cgi? p=smlar . git;a=摘要
4、smlar+海明码(向量相似)
《海量数据,海明(simhash)距离高效检索(smlar) - 阿里云RDS PosgreSQL最佳实践》
5、pg_similarity
https://github.com/eulerto/pg_similarity
00-1010向量相似度和元素重叠计算明显不同。基于元素重叠的相似性,可以通过逆序实现,如前一节所述。然而,基于元素向量的相似性,需要用户定义的索引接口。典型的例子有空间距离中GiST指数的计算,图像特征值相似度中imgsmlr插件的计算。
类别2 : 向量相似(类似knn距离)
https://github.com/postgrespro/imgsmlr
原理如下
64*64图像,取16个区域的平均值,生成16个浮点数作为图像特征值。
一个值相似,减法的绝对值最小。
两个值的相似度可以理解为平面坐标,和最小距离(GiST knn距离排序)。
三个值的相似度可以理解为3D坐标中的点,可以找到距离最小的点。
.
16个值相似,与上面类似。Imgsmlr插件使用gist索引接口实现16个元素的向量相似索引排序。
例子
postgres=#\dt_img
表“public.t_img”
列|类型|排序规则|可空|默认
- - - - -
id |整数||notnull|
sig |签名|||
索引:
t_img_pkey'PRIMARYKEY,btree(id)
Idx_t_img_1'gist(sig)数据量
postgres=# selectcount(*)from _ img;
数数
-
319964709
(1低)
Time: 698.075 ms
图像特征值搜索例子,速度杠杠的。(以上使用citus+postgres+128 shard)
postgres=# select * from t_img order by sig <-> '(3.539080, 0.243861, 1.509150, 1.781380, 8.677560, 4.232060, 8.979810, 1.665030, 1.294100, 4.449800, 9.200450, 1.859860, 5.440250, 7.788580, 0.514258, 8.424920)' limit 1; id | sig -----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------ 148738668 | (2.554440, 0.310499, 2.322520, 0.478624, 7.816080, 4.360440, 8.287050, 1.011060, 2.114320, 3.541110, 9.166300, 1.922250, 4.488640, 7.897890, 1.600290, 7.462080) (1 row) Time: 337.301 ms
2 CUBE
https://www.postgresql.org/docs/devel/static/cube.html
a <-> b float8 Euclidean distance between a and b. a <#> b float8 Taxicab (L-1 metric) distance between a and b. a <=> b float8 Chebyshev (L-inf metric) distance between a and b.
计算图片向量相似时,cube比imgsmlr性能稍差,因为cube使用的是float8,而imgsmlr使用的是float4。
例子
cube
postgres=# explain (analyze,verbose,timing,costs,buffers) select * from t_img0 order by sig::Text::cube <-> '(0.435404, 6.602870, 9.050220, 9.379750, 2.483920, 1.534660, 0.363753, 4.079670, 0.124681, 3.611220, 7.127460, 7.880070, 2.574830, 6.778820, 5.156320, 8.329430)' limit 1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.36..0.37 rows=1 width=76) (actual time=147.432..147.434 rows=1 loops=1) Output: id, sig, ((((sig)::text)::cube <-> '(0.435404, 6.60287, 9.05022, 9.37975, 2.48392, 1.53466, 0.363753, 4.07967, 0.124681, 3.61122, 7.12746, 7.88007, 2.57483, 6.77882, 5.15632, 8.32943)'::cube)) Buffers: shared hit=16032 -> Index Scan using idx_t_img0_1 on public.t_img0 (cost=0.36..13824.28 rows=754085 width=76) (actual time=147.430..147.430 rows=1 loops=1) Output: id, sig, (((sig)::text)::cube <-> '(0.435404, 6.60287, 9.05022, 9.37975, 2.48392, 1.53466, 0.363753, 4.07967, 0.124681, 3.61122, 7.12746, 7.88007, 2.57483, 6.77882, 5.15632, 8.32943)'::cube) Order By: (((t_img0.sig)::text)::cube <-> '(0.435404, 6.60287, 9.05022, 9.37975, 2.48392, 1.53466, 0.363753, 4.07967, 0.124681, 3.61122, 7.12746, 7.88007, 2.57483, 6.77882, 5.15632, 8.32943)'::cube) Buffers: shared hit=16032 Planning Time: 0.096 ms Execution Time: 148.905 ms (9 rows)
imgsmlr
postgres=# explain (analyze,verbose,timing,costs,buffers) select * from t_img0 order by sig <-> '(0.435404, 6.602870, 9.050220, 9.379750, 2.483920, 1.534660, 0.363753, 4.079670, 0.124681, 3.611220, 7.127460, 7.880070, 2.574830, 6.778820, 5.156320, 8.329430)' limit 2; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.36..0.37 rows=2 width=72) (actual time=40.284..48.183 rows=2 loops=1) Output: id, sig, ((sig <-> '(0.435404, 6.602870, 9.050220, 9.379750, 2.483920, 1.534660, 0.363753, 4.079670, 0.124681, 3.611220, 7.127460, 7.880070, 2.574830, 6.778820, 5.156320, 8.329430)'::signature)) Buffers: shared hit=2914 -> Index Scan using t_img0_sig_idx on public.t_img0 (cost=0.36..7032.36 rows=754085 width=72) (actual time=40.282..48.179 rows=2 loops=1) Output: id, sig, (sig <-> '(0.435404, 6.602870, 9.050220, 9.379750, 2.483920, 1.534660, 0.363753, 4.079670, 0.124681, 3.611220, 7.127460, 7.880070, 2.574830, 6.778820, 5.156320, 8.329430)'::signature) Order By: (t_img0.sig <-> '(0.435404, 6.602870, 9.050220, 9.379750, 2.483920, 1.534660, 0.363753, 4.079670, 0.124681, 3.611220, 7.127460, 7.880070, 2.574830, 6.778820, 5.156320, 8.329430)'::signature) Buffers: shared hit=2914 Planning Time: 0.091 ms Execution Time: 48.210 ms (9 rows)
cube相比imgsmlr的好处是:cube可以计算任意维度的向量相似,imgsmlr则仅用于计算16维(signation类型)的向量相似
到此,关于“PostgreSQL的相似搜索插件有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/80641.html