使用 ts_vector 列进行文本搜索

Using a ts_vector column for a text search

很难找到关于这个 ts_vector 东西的信息,但是为了加快地址搜索的速度,地址列上的简单索引并不能真正给出令人满意的结果。

为了规避此限制,我尝试将 ts_vector 与以下查询一起使用:

alter table mytable add tsv ts_vector;

update mytable set tsv = to_tsvector(address);

我对这个 ts_vector 列非常不熟悉,但是如果我创建一个 btree 索引(或任何其他索引)而不是查询地址列来查询 ts_vector ,它会加快速度吗改为专栏?

可以,但必须是 GIN 索引:

CREATE INDEX ON mytable USING gin (tsv);

你不需要添加那个额外的列,你也可以这样做:

CREATE INDEX ON mytable USING gin (to_tsvector('english', address));

这样的索引可以与@@运算符一起使用。

postgreSQL 文档似乎表明使用单独的 tsvector 列确实更快:

One advantage of the separate-column approach over an expression index is that it is not necessary to explicitly specify the text search configuration in queries in order to make use of the index. As shown in the example above, the query can depend on default_text_search_config. Another advantage is that searches will be faster, since it will not be necessary to redo the to_tsvector calls to verify index matches. The expression-index approach is simpler to set up, however, and it requires less disk space since the tsvector representation is not stored explicitly.

来源:https://www.postgresql.org/docs/current/textsearch-tables.html