从 PostgreSQL 中的多个表中搜索文本和字符串的优化方法是什么

What is the optimised way to search a text and string from multiple tables in PostgreSQL

我想搜索 2 列,它们位于两个不同的 table 中,一个是 name,另一个是 description,类型为 stringtext分别。

当我在互联网上看到各种博客/东西时,我真的很困惑如何找到最快的获取数据的方法。

每个 table 中有 100K+ 行。

到目前为止我做了什么:我为 table 创建了一个包含描述的 tsvector 列,并使用 GIN 对其进行了索引。但我对如何为名称列执行此操作感到困惑?

我无法使用 ilike '%{keyword}%',因为它不使用索引。

name(字符串类型)也使用全文搜索是否好,或者哪种方法对我的情况最好?

提前致谢

select *
from 
    ((select name as "customId", id as aid 
      from accounts 
      where name ilike '%cust%' limit 10)

     union all

     (select t2."customId", null 
      from t2 
      where t2.tsv @@ to_tsquery('cust') limit 10)
) e2

您使用 UNION ALL 进行搜索的想法很好。

为了加快子串搜索,可以使用三元组索引:

CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE INDEX ON accounts USING gin (name gin_trgm_ops);