我在文档中找不到这个问题的确切答案。如果列是数组类型,是否会对所有输入的值分别进行索引?
我创建了一个包含一个int[]
列的简单表,并在其上放置了一个惟一索引。我注意到我不能添加相同的int数组,这使我相信索引是数组项的组合,而不是每个项的索引。
INSERT INTO "Test"."Test" VALUES ('{10, 15, 20}');
INSERT INTO "Test"."Test" VALUES ('{10, 20, 30}');
SELECT * FROM "Test"."Test" WHERE 20 = ANY ("Column1");
索引对这个查询有帮助吗?
发布于 2012-11-13 07:42:48
现在可以对单个数组元素进行索引。例如:
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上运行。请注意,您需要为每个数组索引构建单独的索引,在我的示例中,我只对第一个元素进行了索引。
https://stackoverflow.com/questions/4058731
复制相似问题