有一个表和一个 gin 索引,插入 1,000,000 个随机数。0 < 数字 < 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 索引?