【问题标题】:Postgres tsearch performance on first query第一次查询的 Postgres tsearch 性能
【发布时间】:2015-08-15 02:10:29
【问题描述】:

我们正在使用自定义文本搜索配置来搜索德语文本,以正确支持复合词。

字典可以在这里找到: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;

字典本身运行良好,但在每个新连接/会话上,使用此配置的第一个查询需要 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。但这引入了一些其他问题,看起来像一个缓存问题。

有什么办法可以减少这个“启动时间”?看起来好像为每个新连接加载/创建了配置,这似乎并不合理。

【问题讨论】:

    标签: performance postgresql indexing full-text-search ispell


    【解决方案1】:

    已知问题 - 加载 ispell 字典很慢(每次在会话中第一次使用字典时都会加载它)。一个很好的解决方案是会话池。其他解决方案是使用共享 ispell 字典 - 由 Tomas Vondra 编写的扩展 - shared_ispell,但我不知道 PostgreSQL 9.2 及更高版本的某些新版本的支持情况如何 - 它在 9.2 上进行了测试。德语可能有问题 - 它是用捷克语测试的。

    其他可能性是使用德语雪球词典 - 它应该会明显更快 - 但结果可能会更糟。从全文配置中删除 German_bon_ispell。

    【讨论】:

    • 感谢您的评论。我厌倦了删除 ispell dict,但结果确实无法使用。德语真的很喜欢复合词(比如“Wandkalender”代表“挂历”),而雪球似乎不适合这些词。所以我想我会坚持使用池化。
    猜你喜欢
    • 2012-11-06
    • 2012-03-27
    • 2020-10-15
    • 1970-01-01
    • 2021-12-11
    • 2023-03-30
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多