Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 字典词排序列表的最佳索引_Postgresql - Fatal编程技术网

Postgresql 字典词排序列表的最佳索引

Postgresql 字典词排序列表的最佳索引,postgresql,Postgresql,我有一个包含两个colun的表:ID序列和“word”,它是德语单词的列表(按sort命令排序,然后复制“ied”): 目标是列出包含特定字符串的所有单词,f.i.: SELECT word FROM german_words WHERE word ~ 'chen'; 对于“word”列,我使用了普通的惟一索引,这样的查询需要1到2秒(1.9M行)。PostgreSQL中是否有一种索引策略可以使其更快?对于此类查询,以下索引将是最佳索引: CREATE INDEX ON words USING

我有一个包含两个colun的表:ID序列和“word”,它是德语单词的列表(按
sort
命令排序,然后复制“ied”):

目标是列出包含特定字符串的所有单词,f.i.:

SELECT word FROM german_words WHERE word ~ 'chen';

对于“word”列,我使用了普通的惟一索引,这样的查询需要1到2秒(1.9M行)。PostgreSQL中是否有一种索引策略可以使其更快?

对于此类查询,以下索引将是最佳索引:

CREATE INDEX ON words USING gin (word gin_trgm_ops);
这要求您安装

从文件中:

F.31.4。索引支持

pg_trgm模块提供GiST和GIN索引运算符类,允许您在文本列上创建索引,以便进行非常快速的相似性搜索。这些索引类型支持上述相似性运算符,另外还支持对LIKE、ILIKE、~和~*查询进行基于三角图的索引搜索


您可能得到的执行计划如下:

+----+---------------------------------------------------------------------------------------------------------------------------+ | | QUERY PLAN | | 1 | Bitmap Heap Scan on words (cost=20.47..70.73 rows=60 width=36) (actual time=0.147..0.432 rows=182 loops=1) | | 2 | Recheck Cond: (word ~ 'chen'::text) | | 3 | Rows Removed by Index Recheck: 2 | | 4 | Heap Blocks: exact=42 | | 5 | -> Bitmap Index Scan on words_word_idx (cost=0.00..20.45 rows=60 width=0) (actual time=0.129..0.129 rows=184 loops=1) | | 6 | Index Cond: (word ~ 'chen'::text) | | 7 | Planning time: 0.133 ms | | 8 | Execution time: 0.476 ms | +----+---------------------------------------------------------------------------------------------------------------------------+ +----+---------------------------------------------------------------------------------------------------------------------------+ ||查询计划| |1 |字位图堆扫描(成本=20.47..70.73行=60宽度=36)(实际时间=0.147..0.432行=182循环=1)| |2 |复核条件:(word~‘chen’::text)| |3 |通过索引重新检查删除的行:2| |4 |堆块:精确=42| |5 |->字_word_idx上的位图索引扫描(成本=0.00..20.45行=60宽度=0)(实际时间=0.129..0.129行=184个循环=1)| |6 |索引条件:(word~‘chen’::text)| |7 |计划时间:0.133毫秒| |8 |执行时间:0.476毫秒| +----+---------------------------------------------------------------------------------------------------------------------------+

我不能100%确定您是否可以使用
uncent
,因为它是为全文搜索词汇表设计的。但是你很可能会使用类似的东西,并做一些类似于
的翻译(重音单词,'aè236;òùèèèèèèèèèèèèèèè。。。表中的
单词和要匹配的字符序列。我会使用一个特定的列
uncentered\u word
来存储计算的信息。非常感谢您的回答、评论和有用的提示!我能把非协调和你的解决方案结合起来。表现上的差异是巨大的,我很高兴:)很高兴能帮上忙! +----+---------------------------------------------------------------------------------------------------------------------------+ | | QUERY PLAN | | 1 | Bitmap Heap Scan on words (cost=20.47..70.73 rows=60 width=36) (actual time=0.147..0.432 rows=182 loops=1) | | 2 | Recheck Cond: (word ~ 'chen'::text) | | 3 | Rows Removed by Index Recheck: 2 | | 4 | Heap Blocks: exact=42 | | 5 | -> Bitmap Index Scan on words_word_idx (cost=0.00..20.45 rows=60 width=0) (actual time=0.129..0.129 rows=184 loops=1) | | 6 | Index Cond: (word ~ 'chen'::text) | | 7 | Planning time: 0.133 ms | | 8 | Execution time: 0.476 ms | +----+---------------------------------------------------------------------------------------------------------------------------+