如何在PostgreSQL访问阵列的内部指标? [英] How to access array internal index with postgreSQL?

查看:134
本文介绍了如何在PostgreSQL访问阵列的内部指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我的(也许平时你)的非优化解决方案:

解决方法PG问题与非优化的内部功能:

Workaround for PG problem with non-optimized internal function:

CREATE FUNCTION unnest_with_idx(anyarray)
RETURNS TABLE(idx integer, val anyelement) AS
$$ 
   SELECT generate_series(1,array_upper($1,1)) as idx, unnest($1) as val;
$$ LANGUAGE SQL IMMUTABLE;

测试:

SELECT idx,val from unnest_with_idx(array[1,20,3,5]) as t;

不过,正如我所说,的非优化的。我不能相信(!!)PostgreSQL所不具备阵列的内部指标...?但在这种情况下,问题是如何直接访问这个指数,其中该 GIN状的内部计数器?

But, as I said, non-optimized. I can't believe (!!) that PostgreSQL doesn't have an internal index for arrays ... ? But in this case, the question is how to directly access this index, where the GIN-like internal counter?

注1:上述解决方案,问题是不一样的你如何创建一个由数组中的每个元素的索引? 。同时不一样的能否PostgreSQL的数组索引列?,因为函数是一个孤立的阵列,而不是一个表指数数组字段。

NOTE1: the solution above and the question is not the same as "how do you create an index by each element of an array?". Also not the same as "Can PostgreSQL index array columns?" because the function is for an isolated array, not for a table index for array fields.

注2(答案后编):数组索引(更流行的术语)或数组下标或阵列柜是,我们可以在语义的路径中使用指内部计数器,累加器条款下一个数组项。我看到的 否PostgreSQL的命令提供给该计数器的直接访问。由于 generate_series()功能, generate_subscripts()函数是一个序列发生器,然后性能接近相同(最好但)。由另一方面 ROW_NUMBER()功能提供了一个行内部计数器的直接访问,但它是关于行的不是数组的,不幸的是性能较差。

NOTE2 (edited after answers): "array indexes" (more popular term) or "array subscripts" or "array counter" are terms that we can use in a semantic path to refer the "internal counter", the accumulator to the next array item. I see that no PostgreSQL command offer a direct access to this counter. As generate_series() function, the generate_subscripts() function is a sequence generator, and the performance is (best but) near the same. By other hand row_number() function offers a direct access to a "internal counter of rows", but it is about rows, not about arrays, and unfortunately the performance is worse.

推荐答案

的PostgreSQL的确实的提供专用的功能,<一个href=\"http://www.postgresql.org/docs/current/interactive/functions-srf.html#FUNCTIONS-SRF-SUBSCRIPTS\"相对=nofollow>生成数组下标的:

PostgreSQL does provide dedicated functions to generate array subscripts:

WITH   x(a) AS ( VALUES ('{1,20,3,5}'::int[]) )
SELECT generate_subscripts(a, 1) AS idx
      ,unnest(a) AS val
FROM   x;

有效它几乎一样@弗兰克的查询,只是没有子查询。结果
再加上它与标不与 1 开始。

无论哪种解决方案适用于对 1维的唯一阵列! (可以很容易地扩展到多个维度)。

Either solution works for for 1-dimensional arrays only! (Can easily be expanded to multiple dimensions.)

功能:

CREATE OR REPLACE FUNCTION unnest_with_idx(anyarray) 
RETURNS TABLE(idx integer, val anyelement) LANGUAGE SQL IMMUTABLE AS
$func$
  SELECT generate_subscripts($1, 1), unnest($1);
$func$;

电话:

SELECT * FROM unnest_with_idx('{1,20,3,5}'::int[]);

还认为:

SELECT * FROM unnest_with_idx('[4:7]={1,20,3,5}'::int[]);

更多关于数组下标此相关的问题

如果你真的想归标(从1开始),我会用:

If you actually want normalized subscripts (starting with 1), I'd use:

SELECT generate_series(1, array_length($1,1)) ...

这几乎是你已经查询,只需用的 的> array_length()>代替 array_upper() - 这将失败,非标标。

That's almost the query you had already, just with array_length() instead of array_upper() - which would fail with non-standard subscripts.

我跑了1000一个int数组迄今这里psented所有查询$ P $的快速测试。他们都执行大致相同(〜3.5毫秒) - 除了 ROW_NUMBER()上一个子查询(〜7.5毫秒) - 如预期,因为子查询

I ran a quick test on an array of 1000 int with all queries presented here so far. They all perform about the same (~ 3,5 ms) - except for row_number() on a subquery (~ 7,5 ms) - as expected, because of the subquery.

除非你使用非标准的指数下标操作,使用新的 与序数 而不是:

Unless you operate with non-standard index subscripts, use the new WITH ORDINALITY instead:

  • PostgreSQL unnest() with element number

这篇关于如何在PostgreSQL访问阵列的内部指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆