阵列重叠计数

Array overlap count

如何计算作为参数传递的数组与数组类型的列之间的重叠计数。

 SELECT id1, overlap_count(ary, '{32,56,72,...,996}') FROM tbl

它在 ary 字段中检查有多少元素与作为参数传递的数组匹配。

我的问题有两个。我的 table 是这样的:ID1,ID2,ary 我想尽可能使用多维数组(每个数组可以是任意维度),但怀疑它会更复杂或非常慢。

如果不是,我将不得不使用一维数组的重复记录,如下所示:

  | ID1  | ID2 |  ary   |
  | 5    | 7   |  {...} |
  | 43   | 611 |  {...} |
  | 5    | 7   |  {...} |

这是一个简单的作业函数。

create or replace function overlap_count(arr_a integer[], arr_b integer[]) 
returns integer language sql immutable as
$$
 select count(*)::integer from unnest(arr_a) a inner join unnest(arr_b) b on a = b;
$$;