postgresql - 索引 postgresql 数组列以进行大于/小于比较?

标签 postgresql indexing

我可以在 Postgresq 9.1 中索引数组列以支持:

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

在哪里a是一个整数数组(其中的行具有一些值,例如 {90,110} ,因此查询不会返回空值。)


详情

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

正在运行 explain analyze在上面的查询中产生:

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

背景

以下问题描述了一种方法,但似乎不适用于非固定长度数组:

Can PostgreSQL index array columns?

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

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

最佳答案

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

它还允许您运行扫描值范围的类似查询:

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)

关于postgresql - 索引 postgresql 数组列以进行大于/小于比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19877341/

相关文章:

sql - Postgres 不接受列名前的表别名

python - 多索引/高级索引,其中级别不是 (!=) 值

postgresql - 相同的 postgres 查询在不同的数据库中产生不同的查询计划

python - 每次运行 COPY 命令时是否应该创建表

php - 如何在 PHP 中使用 Postgresql 白名单和准备语句?

postgresql - 看不到 Liferay 6.2 的网页内容列表

sql - 具有时间有效性的 Oracle 表的主键违规

linux - Zoom Search Engine 类似搜索引擎,但适用于 Linux/UNIX

jdbc - 在ElasticSearch中使用JDBC River(MYSQL)进行多索引

macos - 如何从 macOS Sierra 中完全删除 Postgres