是的,您可以为数组建立索引,但是必须使用数组运算符和GIN-index类型。
例:
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
注意
在许多情况下,似乎需要gin__int_ops选项
create index <index_name> on <table_name> using GIN (<column> gin__int_ops)
我还没有看到没有gin__int_ops选项就可以与&&和@>运算符一起使用的情况
jsonb
和索引?postgresql.org/docs/9.5/static/functions-json.html和postgresql.org/docs/9.5/static/datatype-json.html#JSON-INDEXING