如何让PostgreSQL使用gin_trgm_ops索引进行相等比较

How to make PostgreSQL use gin_trgm_ops index for equality comparison

有一个table和一个gin索引,插入1,000,000个随机数。 0 < number < 100,000.
测试两个等效查询

create table Test
(
    id   serial primary key,
    code varchar(255) not null
);
create index Test_code_gin on Test using gin (code gin_trgm_ops);

-- Test1
explain analyse
select * from Test where code like '1234';

-- Test2
explain analyse 
select * from Test where code = '1234';

测试 1 使用 gin_trgm_ops 索引,执行时间:1.640 毫秒;
Test2 不使用索引,执行时间:24.531 ms;

我该怎么做才能使 PostgreSQL 使用索引?或者修改ORM策略和我的SQL语句?或者简单地添加一个 BTree 索引?

该功能是在 v14 中添加的。因此,您可以将 PostgreSQL 升级到 v14,然后将 pg_trgm 升级到其最新版本(使用 pg_upgrade 升级不会自动执行此操作)。

但我只想创建 btree 索引,因为对于相等性来说,这应该比使用三元组更快。仅仅为了访问劣质实现而升级并不是胜利,除非无法忍受索引所需的额外 space。