Index a postgresql array column for greater than / less than comparisons?

Can I index array columns in Postgresq 9.1 to support:

select * from t where 100 < any(a) and 100 > any(a)

Where a is an integer array (with rows that have some values like {90,110} , so the query isn't returning empty.)


Details

create table t ( a integer[] )
insert into t values ('{90,110}'::integer[])

Running explain analyze on the query above yields:

Seq Scan on t  (cost=0.00..1.07 rows=2 width=32) (actual time=0.009..0.009 rows=1 loops=1)
  Filter: ((100 < ANY (a)) AND (100 > ANY (a)))
Total runtime: 0.023 ms

Background

The following question describes an approach but doesn't seem to work for non-fixed-length arrays:

Can PostgreSQL index array columns?

The Postgres docs describe the built-in GIN operators, but they don't appear to support greater than/less than operations:

As an example, the standard distribution of PostgreSQL includes GIN operator classes for one-dimensional arrays, which support indexed queries using these operators: <@, @>, =, &&


You could create an index on a function that returns the bounds of your column as an int4range:

create or replace function intarray2int4range(arr int[]) returns int4range as $$
  select int4range(min(val), max(val) + 1) from unnest(arr) as val;
$$ language sql immutable;

Example:

create table t (a int[]);
insert into t
select array[i - j % 5, i - j % 3, i, i + j % 3, i + j % 5]
from generate_series(0,1000) i, generate_series(0,100) j;
create index on t using gist(a);
vacuum analyze t;

Yields:

explain analyze select * from t where 20 <@ intarray2int4range(a) limit 5;
                                                               QUERY PLAN                                                               
----------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.53..16.55 rows=5 width=41) (actual time=0.056..0.060 rows=5 loops=1)
   ->  Index Scan using t_intarray2int4range_idx on t  (cost=0.53..1669.65 rows=521 width=41) (actual time=0.055..0.058 rows=5 loops=1)
         Index Cond: (20 <@ intarray2int4range(a))
 Total runtime: 0.095 ms
(4 rows)

It would also allow you to run similar queries that scan for ranges of values:

explain analyze select * from t where '[20,30]'::int4range && intarray2int4range(a) limit 5;
                                                               QUERY PLAN                                                                
-----------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.53..11.82 rows=5 width=41) (actual time=0.125..0.130 rows=5 loops=1)
   ->  Index Scan using t_intarray2int4range_idx on t  (cost=0.53..3499.66 rows=1550 width=41) (actual time=0.123..0.126 rows=5 loops=1)
         Index Cond: ('[20,31)'::int4range && intarray2int4range(a))
 Total runtime: 0.169 ms
(4 rows)
链接地址: http://www.djcxy.com/p/87222.html

上一篇: postgres(Rails)中的数组列的Uniq索引

下一篇: 索引postgresql数组列的大于/小于比较?