我在一个表中有100.000个句子的列表,使用pg_trgm可以使用GIN / GIST索引快速获取字符串“ super cool”中最接近的一个。参见官方示例:
https://www.postgresql.org/docs/11/pgtrgm.html
可悲的是,我想要相反的情况,我想先找到最不同的一个,但是在DESC时不使用GIN / GIST索引,因此它非常慢。
SELECT t, 'super cool' <-> t AS dist
FROM test_trgm
ORDER BY dist DESC LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
我该怎么办?从源代码重建pg_trgm?怎么样 ?
我已经定义了以下索引:
CREATE INDEX
users_search_idx
ON
auth_user
USING
gin(
username gin_trgm_ops,
first_name gin_trgm_ops,
last_name gin_trgm_ops
);
Run Code Online (Sandbox Code Playgroud)
我正在执行以下查询:
PREPARE user_search (TEXT, INT) AS
SELECT
username,
email,
first_name,
last_name,
( -- would probably do per-field weightings here
s_username + s_first_name + s_last_name
) rank
FROM
auth_user,
similarity(username, $1) s_username,
similarity(first_name, $1) s_first_name,
similarity(last_name, $1) s_last_name
WHERE
username % $1 OR
first_name % $1 OR
last_name % $1
ORDER BY
rank DESC
LIMIT $2;
Run Code Online (Sandbox Code Playgroud)
该auth_user表有620万行.
查询的速度似乎在很大程度上取决于查询可能返回的结果数similarity.
通过增加相似性阈值有set_limit帮助,但通过消除部分匹配来降低结果的有用性.
有些搜索在200ms内返回,其他搜索需要大约10秒. …
我想在postgresql中结合一种模糊搜索来实现全文搜索。对于我的测试区,我跟进了这篇文章:https ://blog.lateral.io/2015/05/full-text-search-in-milliseconds-with-postgresql/ 一切正常。但有时我在搜索字符串中有空格和没有空格的搜索案例,如下所示:
在我的“标题”栏中有一个像“ test123 ”这样的条目。我的搜索字符串看起来像' test 123 ',里面有一个空格。我怎样才能在这个测试用例中获得成功?
我的 search-sql-query 看起来像:
SELECT *
FROM test, plainto_tsquery('test:*&123:*') as q
WHERE (tsv @@ q)
result: 0 rows
Run Code Online (Sandbox Code Playgroud)
所以我试图弄清楚是否可以将 pg_trgm 与 ts_vector 结合使用,但我找不到解决方案。你有想法吗?
我正在尝试在 RDS postgres (13) 实例上设置pg_trgm.word_similarity_threshold GUC 参数。
我尝试使用部署后 SQL 脚本设置它:
SET pg_trgm.word_similarity_threshold = 0.5;
SELECT pg_reload_conf();
Run Code Online (Sandbox Code Playgroud)
但这会导致错误:Npgsql.PostgresException (0x80004005): 42501: permission denied for function pg_reload_conf
我还尝试通过 terraform 将其传递到参数列表中来设置它:
SET pg_trgm.word_similarity_threshold = 0.5;
SELECT pg_reload_conf();
Run Code Online (Sandbox Code Playgroud)
这会导致错误:Error modifying DB Parameter Group: InvalidParameterValue: Could not find parameter with name: pg_trgm.word_similarity_threshold
尽管 pg_trgm 是受支持的扩展,但该参数在 postgres 参数组中似乎不可用。有什么方法可以在我的 AWS Postgres RDS 中设置此参数吗?
我可以采取任何其他步骤来加快查询执行速度吗?
\n我有一个超过 100m 行的表,我需要搜索匹配的字符串。为此,我检查了两个选项:
\n@@(to_tsquery 或 plainto_tsquery)进行比较我发现我可以使用索引来提高性能。\n对于我的 GiST 索引,我尝试siglen从小数字增加到 2024,但由于某种原因 Postgres 使用512而不是更高。
CREATE INDEX trgm_idx_512_gg ON table USING GIST (name gist_trgm_ops(siglen=512));\nRun Code Online (Sandbox Code Playgroud)\n询问:
\nSELECT name, similarity(name, '\xd0\xbd\xd0\xbe\xd1\x83\xd1\x82\xd0\xb1\xd1\x83\xd0\xba MSI GF63 Thin 10SC 086XKR 9S7 16R512 086') as sm\nFROM table\nWHERE name % '\xd0\xbd\xd0\xbe\xd1\x83\xd1\x82\xd0\xb1\xd1\x83\xd0\xba MSI GF63 Thin 10SC 086XKR 9S7 16R512 086' \nRun Code Online (Sandbox Code Playgroud)\nEXPLAIN输出:
Bitmap Heap Scan on …Run Code Online (Sandbox Code Playgroud) 我的PostgreSQL 9.3 DB中有3亿个地址,我想用pg_trgm模糊搜索行.最终目的是实现与谷歌地图搜索一样的搜索功能.
当我使用pg_trgm搜索这些地址时,获得结果需要大约30秒.有许多行符合0.3的默认相似性阈值条件,但我只需要大约5或10个结果.我创建了一个trigram GiST索引:
CREATE INDEX addresses_trgm_index ON addresses USING gist (address gist_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
这是我的查询:
SELECT address, similarity(address, '981 maun st') AS sml
FROM addresses
WHERE address % '981 maun st'
ORDER BY sml DESC
LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
生产环境的测试表已被删除.我显示了EXPLAIN测试环境的输出.大约有700万行,它需要大约1.6秒来获得结果.拥有3亿,需要30多个.
ebdb=> explain analyse select address, similarity(address, '781 maun st') as sml from addresses where address % '781 maun st' order by sml desc limit 10;
QUERY PLAN
————————————————————————————————————————————————————————————————————————————————
Limit (cost=7615.83..7615.86 rows=10 width=16) (actual time=1661.004..1661.010 rows=10 loops=1)
-> Sort (cost=7615.83..7634.00 rows=7268 …Run Code Online (Sandbox Code Playgroud) postgresql pattern-matching nearest-neighbor bigdata pg-trgm
有一个表和一个gin索引,插入1,000,000个随机数。0 < 数量 < 100,000。
测试两个等效查询
create table Test
(
id serial primary key,
code varchar(255) not null
);
create index Test_code_gin on Test using gin (code gin_trgm_ops);
-- Test1
explain analyse
select * from Test where code like '1234';
-- Test2
explain analyse
select * from Test where code = '1234';
Run Code Online (Sandbox Code Playgroud)
测试1使用gin_trgm_ops索引,执行时间:1.640 ms;
Test2不使用索引,执行时间:24.531 ms;
我该怎么做才能让 PostgreSQL 使用索引?或者修改ORM策略和我的SQL语句?或者干脆添加一个BTree索引?
将 pg_trgm.word_similarity_threshold 设置为 0.2;降低当前会话的阈值,但不会降低数据库的阈值。我需要降低支持拼写错误的门槛。
我需要为大量公司(超过 80,000,000 家)进行搜索时自动完成。公司名称应包含以这样的搜索查询开头的单词
+-------+----------------------------------+
| term | results |
+-------+----------------------------------+
| gen | general motors; general electric |
| geno | genoptix; genomic health |
| genom | genoma group; genomic health |
+-------+----------------------------------+
Run Code Online (Sandbox Code Playgroud)
该pg_trgm模块和GIN索引实现类似行为,但并没有解决我的问题。
例如,ElasticSearch 具有完全符合我的要求的Edge NGram Tokenizer功能。
从文档:
The edge_ngram tokenizer first breaks the text down into words
whenever it encounters one of a list of specified characters,
then it emits N-grams of each word
where the start of the N-gram is …Run Code Online (Sandbox Code Playgroud) 我有一个 Django 应用程序和一个 Postgresql 数据库(正在生产中)。现在我想为 Postgres 安装 pg_trgm 扩展。但我找不到任何从 Django 应用程序安装它的分步说明。我有超级用户身份。怎样做才正确呢?
Postgres 允许使用pg_trgm 模块使用三元组索引。
这是他们在“索引支持”部分提供的示例代码:
CREATE TABLE test_trgm (t text);
CREATE INDEX trgm_idx ON test_trgm USING GIST (t gist_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
这是我想出的迁移:
class AddTitleTrigramIndexToContacts < ActiveRecord::Migration[5.1]
def change
enable_extension 'pg_trgm'
execute "CREATE INDEX contacts_title_trigram_ix ON contacts USING GIST (title gist_trgm_ops);"
end
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来添加此迁移?我什至不确定这是否正确。
我正在尝试使用扩展来加速Postgres 中的一些文本匹配pg_trgm:
CREATE TABLE test3 (id bigint, key text, value text);
insert into test3 values (1, 'first 1', 'second 3');
insert into test3 values (2, 'first 1', 'second 2');
insert into test3 values (2, 'first 2', 'second 3');
insert into test3 values (3, 'first 1', 'second 2');
insert into test3 values (3, 'first 1', 'second 3');
insert into test3 values (4, 'first 2', 'second 3');
insert into test3 values (4, 'first 2', 'second 3');
insert into …Run Code Online (Sandbox Code Playgroud) postgresql performance database-performance query-performance pg-trgm
pg-trgm ×12
postgresql ×11
performance ×2
amazon-rds ×1
bigdata ×1
django ×1
indexing ×1
similarity ×1
sql ×1
terraform ×1
trigram ×1
tsvector ×1