如果安装了“
intarray
“延伸。让我们来测试一下:
drop extension intarray;
explain analyze
select * from test_intarray where codes @> array[123];
-- Uses "Bitmap Index Scan on test_intarray_idx"
@>
,而索引是为使用泛型数组运算符而设计的。这可以通过使用模式限定运算符来证明:
create extension intarray;
explain analyze
select * from test_intarray where codes @> array[123];
-- Uses "Seq Scan on test_intarray"
explain analyze
select * from test_intarray where codes operator(pg_catalog.@>) array[123];
-- Uses "Bitmap Index Scan on test_intarray_idx"
有关详细信息,请参阅此讨论:
Overloaded && operator from intarray module prevents index usage.
如果您还想利用“intarray”扩展,可以在创建索引时指定它自己的运算符类“gin_uint_ops”(而不是默认的“array_ops”):
create index test_intarray_idx2 on test_intarray using GIN (codes gin__int_ops);
explain analyze
select * from test_intarray where codes @> array[123];
-- Uses "Bitmap Index Scan on test_intarray_idx2"