Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Database 如何在postgresql 9.4中获取数组的最后25项?_Database_Postgresql - Fatal编程技术网

Database 如何在postgresql 9.4中获取数组的最后25项?

Database 如何在postgresql 9.4中获取数组的最后25项?,database,postgresql,Database,Postgresql,假设我需要获取此数组中的最后3项 {1,2,3,4,5,6,7,8,9,10} 当阵列中有数百万项时,如何使用高性能方法实现这一点 {8,9,10} 从拥有1600万个项目的阵列中获取最后25个项目需要8秒钟。我认为应该有比这快得多的方法。尝试一个片段: SELECT * FROM unnest((select arraycolumn from table where id=1)) WITH ORDINALITY as t(a1, num) order by t.num desc limit

假设我需要获取此数组中的最后3项

{1,2,3,4,5,6,7,8,9,10}
当阵列中有数百万项时,如何使用高性能方法实现这一点

{8,9,10}
从拥有1600万个项目的阵列中获取最后25个项目需要8秒钟。我认为应该有比这快得多的方法。

尝试一个片段:

SELECT * FROM unnest((select arraycolumn from table where id=1)) WITH ORDINALITY as t(a1, num) order by t.num desc limit 25;
试一试:

SELECT * FROM unnest((select arraycolumn from table where id=1)) WITH ORDINALITY as t(a1, num) order by t.num desc limit 25;

到目前为止,您已经尝试了哪些内容,为什么您认为您尝试的内容存在性能问题?在Postgres中首先使用包含数百万项的数组是愚蠢的。把它做成一张表。是的,我现在明白了,所以我绝对不会那样做。到目前为止,你尝试了什么,为什么你认为你尝试的方法有性能问题?在Postgres中首先使用包含数百万项的数组是愚蠢的。把它做成一张表。是的,我现在就知道了,所以我绝对不会那样做。谢谢你,克林……是的,这比220毫秒执行的速度快得多。有没有更好的方法把这个查询运行时间拉到100毫秒以下?我不这么认为。顺便说一句,在我的1600万整数数组的机器上,它需要92毫秒。我想你的电脑会有所不同……再次感谢你。尝试一下这个小改进:从select arraycolumn from table(id=1 suba)中选择[array_lengtha,1-24:2147483647];那会稍微贵一点faster@cinfis:并使用array_upper而不是array_length,这可能是错误的。谢谢Klin…是的,这比220毫秒内执行的速度快得多。有没有更好的方法将此查询运行时间拉到100毫秒以下?我不这么认为。顺便说一句,在我的1600万整数数组的机器上,它需要92毫秒。我想你的电脑会有所不同……再次感谢你。尝试一下这个小改进:从select arraycolumn from table(id=1 suba)中选择[array_lengtha,1-24:2147483647];那会稍微贵一点faster@cinfis:并使用array_upper而不是array_length,这可能是错误的。