Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays PostgreSQL:比较两个数组,忽略排序_Arrays_Postgresql - Fatal编程技术网

Arrays PostgreSQL:比较两个数组,忽略排序

Arrays PostgreSQL:比较两个数组,忽略排序,arrays,postgresql,Arrays,Postgresql,我想比较两个数组,忽略元素和副本的顺序 我可以在两个方向进行遏制检查,但是否有更好/更快的解决方案 select * from my_table where my_table.a1 @> my_table.a2 and my_table.a2 @> my_table.a1 使用非最新和独特的示例: t=# create or replace function so62(a1 anyarray,a2 anyarray) ret

我想比较两个数组,忽略元素和副本的顺序

我可以在两个方向进行遏制检查,但是否有更好/更快的解决方案

select * from my_table where my_table.a1 @> my_table.a2 and
                             my_table.a2 @> my_table.a1

使用非最新和独特的示例:

t=# create or replace function so62(a1 anyarray,a2 anyarray) returns boolean as
$$
declare
 _r boolean := false;
 _a text;
begin
  with p as (select distinct unnest(a1) order by 1) select array_agg(unnest) into _a from p;
  with p as (select distinct unnest(a2) order by 1) select array_agg(unnest)::text = _a into _r from p;
  return _r;
end;
$$ language plpgsql;
CREATE FUNCTION
t=# with c as (
  select '{1,null,2,2}'::int[] a1,'{2,1,null}'::int[] a2
) 
select a1 @> a2, a2 @> a1,so62(a1,a2) 
from c;
 ?column? | ?column? | so62
----------+----------+------
 f        | f        | t
(1 row)
(函数在@poz通知后更新,它需要两个
,除了用于比较的

测试:

t=# create or replace function so62(a1 anyarray,a2 anyarray) returns boolean as
$$
declare
 _r boolean := false;
 _a text;
begin
  with p as (select distinct unnest(a1) order by 1) select array_agg(unnest) into _a from p;
  with p as (select distinct unnest(a2) order by 1) select array_agg(unnest)::text = _a into _r from p;
  return _r;
end;
$$ language plpgsql;
CREATE FUNCTION
t=# with c as (
  select '{1,null,2,2}'::int[] a1,'{2,1,null}'::int[] a2
) 
select a1 @> a2, a2 @> a1,so62(a1,a2) 
from c;
 ?column? | ?column? | so62
----------+----------+------
 f        | f        | t
(1 row)

在我的示例中,这两个变体似乎是最快的(甚至比
@>
+
还要快,如果它是一个一维整数数组,里面没有空值,那么你可以使用它对内容进行排序,删除重复项,然后进行比较。我不知道这是否会快些。另一种方法是
取消测试
并使用set,这可能会快一些,也可能会慢一些,具体取决于你的数据,康迪选项和索引。您还需要以两种方式执行
之外的操作。从技术上讲,您是对的,如果数组可以包含
NULL
值,这是为数不多的几种可能的方法之一,但是如果它不包含任何
NULL
s,这也可以是。是的,它是-但是我从问题中假设为NULL,我的逻辑是:if not NULL not提到了,就可以有了nulls@pozs谢谢你的通知!为了避免除了两次投稿以外的文字