【问题标题】:postgres count with like condition具有类似条件的postgres计数
【发布时间】:2026-01-15 01:50:01
【问题描述】:

我是 Postgres 的新手,并试图弄清楚如何在 where 子句中使用“Like”来提高计数查询的性能。

    select    count(*) 
    from master_data 
    where name ilike '%<userInput>%';

估计值我什至没问题。我用谷歌搜索了几个链接,发现我们可以通过使用以下查询快速获得整个表的计数。

 SELECT reltuples AS approximate_row_count FROM pg_class WHERE relname = '<TableName>'; 

但是对于类似条件的计数,我仍然无法弄清楚。 请建议。以下是我已经提到的链接。

https://wiki.postgresql.org/wiki/Count_estimate

https://www.citusdata.com/blog/2016/10/12/count-performance/#dup_counts_estimated_filtered

Postgres 版本 - 9.5.4

【问题讨论】:

标签: sql postgresql count


【解决方案1】:

您必须使用 LIKE 吗? 也许可以用 IN 做点什么? http://www.postgresqltutorial.com/postgresql-in/

【讨论】:

    【解决方案2】:

    您可能对三元组索引感兴趣。这可以专门处理带有通配符的like。你可以从documentation开始。

    【讨论】:

    • 感谢您的意见。将对此进行探索。