sql - ROW_NUMBER 条查询数据是否需要获取整个结果集?

标签 sql postgresql

我正在尝试编写不使用偏移量的查询(因为我刚刚了解到偏移量会获取所有导致性能开销的数据)。带有 ROW_NUMBER 窗口函数。例如:

SELECT id
FROM(
SELECT id, ROW_NUMBER() over (order by id) rn
FROM users) sq
WHERE rn > 1000

它是否需要像使用 offset 1000 那样获取所有行?我的意思是,使用这样的查询而不是

SELECT if
FROM users
OFFSET 1000

?我是否在处理大量数据时获得了性能提升?

最佳答案

查看 window function文档。窗口函数在获取之后对结果集进行操作:

Window functions are permitted only in the SELECT list and the ORDER BY clause of the query. They are forbidden elsewhere, such as in GROUP BY, HAVING and WHERE clauses. This is because they logically execute after the processing of those clauses. Also, window functions execute after regular aggregate functions. This means it is valid to include an aggregate function call in the arguments of a window function, but not vice versa.

关于sql - ROW_NUMBER 条查询数据是否需要获取整个结果集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31191354/

相关文章:

c# - 在配置文件中选择不同类型的数据库

sql - 一张通用表

postgresql - 使用postgresql的ORDER BY、OFFSET和LIMIT优化SELECT查询

PostgreSQL CREATE TEMPORARY TABLE 在 plpgsql 函数中

mysql - MySql中多行的总和

mysql - 用于匹配子查询的 SQL 查询

c# - 选择匹配字符串部分的所有行

postgresql - 有没有办法知道表中的最后一个提交值?

sql - 相当于 Amazon Redshift 中的 T-SQL 表值构造函数?

sql - 从 json[] 中删除了最后一个元素?