为大于/小于比较的postgresql数组列编制索引? 我可以在Postgresq 9.1中索引数组列以支持:

为大于/小于比较的postgresql数组列编制索引? 我可以在Postgresq 9.1中索引数组列以支持:,postgresql,indexing,Postgresql,Indexing,从t中选择*100anya 其中a是一个整数数组,其中的行具有一些值,如{90110},因此查询不会返回空 细节 对上述查询运行explain ANALYSE将产生: 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

从t中选择*100anya

其中a是一个整数数组,其中的行具有一些值,如{90110},因此查询不会返回空

细节 对上述查询运行explain ANALYSE将产生:

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
出身背景 以下问题介绍了一种方法,但似乎不适用于非固定长度数组:

描述了内置的GIN操作符,但它们似乎不支持大于/小于操作:

例如,PostgreSQL的标准发行版包括GIN 一维数组的运算符类,支持索引 使用以下运算符进行查询:,=&&


您可以在函数上创建索引,该函数将列的边界作为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;
例如:

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;
收益率:

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)

您可以在函数上创建索引,该函数将列的边界作为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;
例如:

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;
收益率:

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)

请解释分析和模式定义,以及PostgreSQL版本。问题不清楚。选择100<'{90110}'::int[];产生一个错误。也许你真的在寻找一种范围类型?选择100@CraigRinger Updated,谢谢。@Denis更新了问题,谢谢。一般来说,可以在本列中找到任意大小的整数列表,而不仅仅是边界。人们可以查询任意数量的相等和/或更大/更小的比较。。。您正在尝试为此数组中的任何元素大于值“a”的任何元素编写快速、可索引的测试任何元素可能不同的元素小于值“b”?请解释分析和架构定义以及PostgreSQL版本。问题尚不清楚。选择100<'{90110}'::int[];产生一个错误。也许你真的在寻找一种范围类型?选择100@CraigRinger Updated,谢谢。@Denis更新了问题,谢谢。一般来说,可以在本列中找到任意大小的整数列表,而不仅仅是边界。人们可以查询任意数量的相等和/或更大/更小的比较。。。您正在尝试为此数组中的任何元素编写一个快速、可索引的测试,该测试的值大于“a”值。任何元素(可能是其他元素)都小于值“b”?