我们正在使用自定义文本搜索配置来搜索德语文本,以正确支持复合词。
可以在这里找到该词典:http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/ (ispell-german-compound.tar.gz)。
字典已转换为 UTF8,我使用以下脚本将配置添加到数据库:
DROP TEXT SEARCH DICTIONARY IF EXISTS german_bon_ispell CASCADE;
DROP TEXT SEARCH DICTIONARY IF EXISTS german_bon_stem CASCADE;
CREATE TEXT SEARCH CONFIGURATION german_bon (copy=german);
CREATE TEXT SEARCH DICTIONARY german_bon_stem (
TEMPLATE = snowball,
Language = german,
StopWords = german
);
CREATE TEXT SEARCH DICTIONARY german_bon_ispell (
TEMPLATE = ispell,
dictfile = german,
afffile = german,
StopWords = german
);
ALTER TEXT SEARCH CONFIGURATION german_bon
ALTER MAPPING FOR
asciiword,word,numword,numhword,hword_asciipart,hword_part,hword_numpart
WITH german_bon_ispell, german_bon_stem;
字典本身工作得很好,但在每个新连接/ session 上,使用此配置的第一个查询需要 1-2 秒。每隔约 1-3 毫秒。
对于英语词典也可以观察到这种效果,但没有那么剧烈:
db=# \timing
Timing is on.
db=# select ts_debug('english', 'Book');
ts_debug
-----------------------------------------------------------------------
(asciiword,"Word, all ASCII",Book,{english_stem},english_stem,{book})
(1 row)
Time: 6,977 ms
db=# select ts_debug('english', 'Book');
ts_debug
-----------------------------------------------------------------------
(asciiword,"Word, all ASCII",Book,{english_stem},english_stem,{book})
(1 row)
Time: 2,258 ms
db=# select ts_debug('german_bon', 'Buch');
ts_debug
---------------------------------------------------------------------------------------------------
(asciiword,"Word, all ASCII",Buch,"{german_bon_ispell,german_bon_stem}",german_bon_ispell,{buch})
(1 row)
Time: 916,286 ms
db=# select ts_debug('german_bon', 'Buch');
ts_debug
---------------------------------------------------------------------------------------------------
(asciiword,"Word, all ASCII",Buch,"{german_bon_ispell,german_bon_stem}",german_bon_ispell,{buch})
(1 row)
Time: 1,240 ms
db=#
我目前知道的唯一解决方法是使用持久连接/连接池,我们为此使用 pgbouncer。但这给客户端带来了一些其他问题(PHP>PDO>Doctrine),看起来像是缓存问题。
有什么办法可以减少这个“启动时间”吗?看起来好像是为每个新连接加载/创建配置,这似乎不合理。
最佳答案
这是一个已知问题 - 加载 ispell 字典很慢(每次在 session 中第一次使用字典时都会加载它)。一种好的解决方案是 session 池。其他解决方案是使用共享 ispell 字典 - 由 Tomas Vondra 编写的扩展 - shared_ispell ,但我不知道 PostgreSQL 9.2 及更高版本的某些新版本的支持情况如何 - 它是在 9.2 上测试的。德语可能有问题 - 它是用捷克语测试的。
另一种可能性是使用德语雪球词典 - 它应该明显更快 - 但结果可能更糟。从全文配置中删除 german_bon_ispell。
关于performance - Postgres tsearch 在第一个查询上的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576514/