首页 > 解决方案 > BIGINT 列的 PostgreSQL GIN 索引

问题描述

我有tbl带有列的表 - 数据 TEXT - fk_id BIGINT - 文本 TEXT

有超过 1 亿条记录,fk_id 有大约 1K 不同的值。我需要像这样运行查询

SELECT * FROM tbl WHERE fk_id=12345 AND text LIKE '%abcdef%

我尝试使用扩展GIN索引btree_gingin_trgm_ops

CREATE EXTENSION pg_trgm; CREATE EXTENSION btree_gin;

CREATE INDEX on tbl USING GIN (fk_id, text gin_trgm_ops)

但查询分析器忽略fk_id查询列

explain select * from tbl where fk_id = 12345 and text like '%abcdef%' limit 10;

Limit  (cost=28.00..32.02 rows=1 width=90)
->  Bitmap Heap Scan on tbl  (cost=28.00..32.02 rows=1 width=90)
        Recheck Cond: (text ~~ '%abcdef%'::text)
        Filter: (fk_id = 12345)
        ->  Bitmap Index Scan on table_fk_id_text_idx  (cost=0.00..28.00 rows=1 width=0)
              Index Cond: (text ~~ '%abcdef%'::text)

但是,如果我对列 fk_id 使用 INT 类型而不是 BIGINT,则索引将按预期工作

tbl - 数据 TEXT - fk_id INT - 文本 TEXT

explain select * from tbl where fk_id = 12345 and text like '%abcdef%' limit 10;

Limit  (cost=36.00..40.02 rows=1 width=90)
  ->  Bitmap Heap Scan on tbl  (cost=36.00..40.02 rows=1 width=90)
        Recheck Cond: ((fk_id = 12345) AND (text ~~ '%abcdef%'::text))
        ->  Bitmap Index Scan on tbl_fk_id_text_idx  (cost=0.00..36.00 rows=1 width=0)
              Index Cond: ((fk_id = 12345) AND (text ~~ '%abcdef%'::text))

所以,我可以将 GIN 与 INT 一起使用,但不能将 GIN 与 BIGINT 一起使用。为什么我不能将 BIGINT 列用于 GIN 索引? 文档说,btree_gin 与 int4(INT) 和 int8(BIGINT) 一起使用。也许还有另一种方法可以解决这个问题?

标签: postgresqlpg-trgm

解决方案


推荐阅读