您好,登錄后才能下訂單哦!
這篇文章主要介紹“PostgreSQL的相似搜索插件有哪些”,在日常操作中,相信很多人在PostgreSQL的相似搜索插件有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL的相似搜索插件有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
類似倒排,以元素重疊度為基準(zhǔn)的相似計算。廣泛應(yīng)用于數(shù)組、全文檢索、字符串、文本特征值、多列任意組合查詢的相似搜索。
代表的PostgreSQL插件如下
https://github.com/postgrespro/rum
https://www.postgresql.org/docs/devel/static/pgtrgm.html
http://sigaev.ru/git/gitweb.cgi?p=smlar.git;a=summary
《海量數(shù)據(jù),海明(simhash)距離高效檢索(smlar) - 阿里云RDS PosgreSQL最佳實踐》
https://github.com/eulerto/pg_similarity
向量相似與元素重疊度計算,顯然是不同的,基于元素的重疊度相似,可以利用倒排來實現(xiàn),如上節(jié)描述。而基于元素向量相似,需要用到自定義的索引接口,典型的代表是GiST索引在空間距離上的計算,以及imgsmlr插件在圖像特征值相似方面的計算。
https://github.com/postgrespro/imgsmlr
原理如下
64*64的圖像,取16個區(qū)域的平均值,生成16個浮點數(shù),作為圖像特征值。
一個值求相似,相減絕對值最小。
2個值求相似,可以理解為平面坐標(biāo),求距離最小(GiST knn距離排序)。
3個值求相似,可以理解為3D坐標(biāo)里面的點,求距離最小的點。
...
16個值求相似,與上類似。imgsmlr插件使用gist索引接口實現(xiàn)了16個元素的向量相似索引排序。
例子
postgres=# \d t_img Table "public.t_img" Column | Type | Collation | Nullable | Default --------+-----------+-----------+----------+--------- id | integer | | not null | sig | signature | | | Indexes: "t_img_pkey" PRIMARY KEY, btree (id) "idx_t_img_1" gist (sig)
數(shù)據(jù)量
postgres=# select count(*) from t_img; count ----------- 319964709 (1 row) 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
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類型)的向量相似
到此,關(guān)于“PostgreSQL的相似搜索插件有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。