gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:24:45 24 4
gpt4 key购买 nike

有一张表和一个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';

Test1 使用了 gin_trgm_ops 索引,执行时间:1.640 ms;
Test2 不使用索引,执行时间:24.531 ms;

如何让 PostgreSQL 使用索引?或者修改ORM策略和我的SQL语句?或者简单地添加一个 BTree 索引?

最佳答案

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

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

关于postgresql - 如何让PostgreSQL使用gin_trgm_ops索引进行相等比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70384167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com