3 回答
TA贡献1806条经验 获得超8个赞
CREATE TABLE "Test"("Column1" int[]); INSERT INTO "Test" VALUES ('{10, 15, 20}'); INSERT INTO "Test" VALUES ('{10, 20, 30}'); CREATE INDEX idx_test on "Test" USING GIN ("Column1"); -- To enforce index usage because we have only 2 records for this test... SET enable_seqscan TO off; EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20];
Bitmap Heap Scan on "Test" (cost=4.26..8.27 rows=1 width=32) (actual time=0.014..0.015 rows=2 loops=1) Recheck Cond: ("Column1" @> '{20}'::integer[]) -> Bitmap Index Scan on idx_test (cost=0.00..4.26 rows=1 width=0) (actual time=0.009..0.009 rows=2 loops=1) Index Cond: ("Column1" @> '{20}'::integer[])Total runtime: 0.062 ms
create index <index_name> on <table_name> using GIN (<column> gin__int_ops)
TA贡献1780条经验 获得超5个赞
..PostgreSQL的标准发行版包括一个用于数组的GIN运算符类,它支持使用以下操作符进行索引查询: <@@>=&&
ST_DWithin()
COMMUTATOR
ANY
constant = ANY (array_expression)
=
= ANY()
constant = ANY (array_expression)
array_expression @> ARRAY[constant]
ANY
旁白
integer
int4
int2
int8
NULL
intarray
UNIQUE
TA贡献1757条经验 获得超7个赞
现在可以对单个数组元素进行索引。例如:
CREATE TABLE test (foo int[]);
INSERT INTO test VALUES ('{1,2,3}');
INSERT INTO test VALUES ('{4,5,6}');
CREATE INDEX test_index on test ((foo[1]));
SET enable_seqscan TO off;
EXPLAIN ANALYZE SELECT * from test WHERE foo[1]=1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------
Index Scan using test_index on test (cost=0.00..8.27 rows=1 width=32) (actual time=0.070..0.071 rows=1 loops=1)
Index Cond: (foo[1] = 1)
Total runtime: 0.112 ms
(3 rows)
这至少适用于Postgres 9.2.1。注意,您需要为每个数组索引构建一个单独的索引,在我的示例中,我只对第一个元素进行了索引。
添加回答
举报