gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:44:38 25 4
gpt4 key购买 nike

我正在尝试编写不使用偏移量的查询(因为我刚刚了解到偏移量会获取所有导致性能开销的数据)。带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com