pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
intarray模块提供了一些有用的函数和操作符,用于操作不含 NULL 的整数数组。它还支持使用其中某些操作符进行索引搜索。
如果所提供的数组中包含任何 NULL 元素,所有这些操作都会抛出错误。
这些操作中有许多只对一维数组才有意义。尽管它们也接受更高维度的输入 数组,但数据会被视为按存储顺序排列的线性数组。
intarray 函数和操作符intarray模块提供的函数列在表 F.10中, 操作符列在表 F.11中。
表 F.10. intarray 函数
| Function | Return Type | Description | Example | Result |
|---|---|---|---|---|
icount(int[]) |
int |
number of elements in array | icount('{1,2,3}'::int[]) |
3 |
sort(int[], text dir) |
int[] |
sort array — dir must be asc or desc |
sort('{1,2,3}'::int[], 'desc') |
{3,2,1} |
sort(int[]) |
int[] |
sort in ascending order | sort(array[11,77,44]) |
{11,44,77} |
sort_asc(int[]) |
int[] |
sort in ascending order | ||
sort_desc(int[]) |
int[] |
sort in descending order | ||
uniq(int[]) |
int[] |
remove adjacent duplicates | uniq(sort('{1,2,3,2,1}'::int[])) |
{1,2,3} |
idx(int[], int item) |
int |
index of first element matching item (0 if none) |
idx(array[11,22,33,22,11], 22) |
2 |
subarray(int[], int start, int len) |
int[] |
portion of array starting at position start, len elements |
subarray('{1,2,3,2,1}'::int[], 2, 3) |
{2,3,2} |
subarray(int[], int start) |
int[] |
portion of array starting at position start |
subarray('{1,2,3,2,1}'::int[], 2) |
{2,3,2,1} |
intset(int) |
int[] |
make single-element array | intset(42) |
{42} |
表 F.11. intarray 操作符
| Operator | Returns | Description |
|---|---|---|
int[] && int[] |
boolean |
overlap — true if arrays have at least one common element |
int[] @> int[] |
boolean |
contains — true if left array contains right array |
int[] <@ int[] |
boolean |
contained — true if left array is contained in right array |
# int[] |
int |
number of elements in array |
int[] # int |
int |
index (same as idx function) |
int[] + int |
int[] |
push element onto array (add it to end of array) |
int[] + int[] |
int[] |
array concatenation (right array added to the end of left one) |
int[] - int |
int[] |
remove entries matching right argument from array |
int[] - int[] |
int[] |
remove elements of right array from left |
int[] | int |
int[] |
union of arguments |
int[] | int[] |
int[] |
union of arrays |
int[] & int[] |
int[] |
intersection of arrays |
int[] @@ query_int |
boolean |
true if array satisfies query (see below) |
query_int ~~ int[] |
boolean |
true if array satisfies query (commutator of @@) |
(在 PostgreSQL 8.2 之前,包含操作符 @> 和 <@ 分别称为 @ 和 ~。这些名称仍然可用,但已弃用,最终将被删除。请注意,旧名称与核心几何数据类型以前采用的约定正好相反!)
操作符&&、@>和 <@等价于PostgreSQL内置的 同名操作符,不同之处在于它们只适用于不包含 NULL 的整数数组,而内置 操作符适用于任何数组类型。这一限制使它们在很多情况下比内置操作符更快。
@@和~~操作符用于测试数组是否满足某个 查询,该查询表示为专用数据类型query_int 的一个值。一个查询由若干整数值组成,这些值会与 数组元素进行检查,并且可通过操作符&(AND)、 |(OR)和!(NOT)组合起来。必要时 可以使用括号。例如,查询1&(2|3)可匹配包含 1 且还包含 2 或 3 之一的数组。
intarray为&&、@>、<@和@@操作符以及常规数组相等运算提供索引支持。
提供了两个 GiST 索引操作符类:gist__int_ops(默认使用)适用于小型到中型数据集,而gist__intbig_ops使用更大的签名,更适合为大型数据集建立索引(即包含大量不同数组值的列)。实现使用带有内置有损压缩的 RD 树数据结构。
另有一个非默认的 GIN 操作符类gin__int_ops,支持相同的操作符。
作为经验法则,GIN 索引的搜索速度快于 GiST 索引,但构建或更新较慢; 因此 GIN 更适合静态数据,而 GiST 更适合经常更新的数据。
-- a message can be in one or more “sections”
CREATE TABLE message (mid INT PRIMARY KEY, sections INT[], ...);
-- create specialized index
CREATE INDEX message_rdtree_idx ON message USING GIST (sections gist__int_ops);
-- select messages in section 1 OR 2 - OVERLAP operator
SELECT message.mid FROM message WHERE message.sections && '{1,2}';
-- select messages in sections 1 AND 2 - CONTAINS operator
SELECT message.mid FROM message WHERE message.sections @> '{1,2}';
-- the same, using QUERY operator
SELECT message.mid FROM message WHERE message.sections @@ '1&2'::query_int;
源代码目录contrib/intarray/bench包含一个基准测试 套件。运行方式如下:
cd .../bench createdb TEST psql TEST < ../_int.sql ./create_test.pl | psql TEST ./bench.pl
bench.pl脚本有很多选项,在不带任何参数运行时会显示 这些选项。
所有工作都由 Teodor Sigaev(<teodor@sigaev.ru>)和 Oleg Bartunov(<oleg@sai.msu.su>)完成。更多信息请见 http://www.sai.msu.su/~megera/postgres/gist/。 Andrey Oktyabrski 在添加新函数和新操作方面也做出了很大贡献。
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。