Can PostgreSQL index array columns?

后端 未结 3 2117
半阙折子戏
半阙折子戏 2020-11-22 17:06

I can\'t find a definite answer to this question in the documentation. If a column is an array type, will all the entered values be individually indexed?

I created a

3条回答
  •  广开言路
    2020-11-22 17:46

    It's now possible to index the individual array elements. For example:

    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)
    

    This works on at least Postgres 9.2.1. Note that you need to build a separate index for each array index, in my example I only indexed the first element.

提交回复
热议问题