首页 > 解决方案 > postgres:全文搜索:查找重复文本行的最快方法?

问题描述

:-)

在表格中查找重复文本的最快方法是什么,即表格中的行在一列中的文本在整个表格中至少出现两次?该表包含超过 1.6 亿行。

我有一个包含以下列的表:idmaintextmaintext_token,后者是用to_tsvector(maintext);. 此外,我在 上创建了一个 GIN 索引maintext_token,即create index idx_maintext_tokens on tablename using gin(maintext_token);

目前,我正在使用以下内容,但这需要相当长的时间:我有一个包含以下列的表:idmaintextmaintext_token,后者是用to_tsvector(maintext);. 此外,我在 上创建了一个 GIN 索引maintext_token,即create index idx_maintext_tokens on tablename using gin(maintext_token);

select maintext, count(maintext)
from ccnc
group by maintext
having count(maintext)>1
order by maintext;

我也尝试执行相同的操作,但maintext我没有使用该maintext_token列进行比较:

select maintext_token, count(maintext_token)
from ccnc
group by maintext_token
having count(maintext_token)>1
order by maintext_token;

这两个查询似乎都运行了很长时间,尽管我预计至少第二个查询会快得多,因为 postgres 可以使用索引进行比较。

提前感谢您的任何见解!干杯:)

标签: postgresql

解决方案


你说你想测试是否相等,所以你可能想对文本进行散列,然后搜索散列。您可以使用散列索引来执行此操作,也可以索引文本的散列。我最近在一个相关问题上得到了一些帮助,您可以在此处找到详细信息和比较:

搜索表达式索引


推荐阅读