ROW_NUMBER 没有 ORDER BY
ROW_NUMBER Without ORDER BY
我必须在现有查询中添加行号,以便我可以跟踪已将多少数据添加到 Redis。如果我的查询失败,那么我可以从在其他 table 中更新的那一行开始。
查询以从 table
的 1000 行后开始获取数据
SELECT * FROM (SELECT *, ROW_NUMBER() OVER (Order by (select 1)) as rn ) as X where rn > 1000
查询工作正常。如果有任何方法我可以在不使用 order by 的情况下获得行号。
这里的select 1
是什么?
是否优化了查询,或者我可以通过其他方式进行优化。请提供更好的解决方案。
只尝试 order by 1
。阅读错误消息。然后恢复order by (select 1)
。意识到写这篇文章的人在某个时候已经阅读了错误消息,然后决定正确的做法是欺骗系统不引发错误,而不是意识到错误试图提醒他们的基本事实。
表格没有固有的顺序。如果您想要某种可以依赖的排序形式,您可以为任何 ORDER BY
子句提供足够的确定性表达式,以便每一行都被唯一标识和排序。
其他任何事情,包括欺骗系统使其不发出错误,都是希望系统在不使用提供给您的工具的情况下做一些明智的事情来确保 它做了一些明智的事情 - 一个明确的 ORDER BY
子句。
您可以使用任何文字值
前
order by (select 0)
order by (select null)
order by (select 'test')
等等
请参阅此以获取更多信息
https://exploresql.com/2017/03/31/row_number-function-with-no-specific-order/
无需担心在 ORDER BY
表达式中指定常量。以下内容引自Itzik Ben-Gan
所著Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions(可从微软免费电子书网站免费下载):
As mentioned, a window order clause is mandatory, and SQL Server
doesn’t allow the ordering to be based on a constant—for example,
ORDER BY NULL. But surprisingly, when passing an expression based on a
subquery that returns a constant—for example, ORDER BY (SELECT
NULL)—SQL Server will accept it. At the same time, the optimizer
un-nests, or expands, the expression and realizes that the ordering is
the same for all rows. Therefore, it removes the ordering requirement
from the input data. Here’s a complete query demonstrating this
technique:
SELECT actid, tranid, val,
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
FROM dbo.Transactions;
Observe in the properties of the Index Scan iterator that the Ordered
property is False, meaning that the iterator is not required to return
the data in index key order
上面的意思是当你使用constant ordering时不执行。我强烈建议阅读这本书,因为 Itzik Ben-Gan
深入描述了 window 函数的工作原理以及如何在使用它们时优化各种情况。
What is select 1 here?
在这种情况下,查询的作者并没有真正考虑任何特定的排序。
ROW_NUMBER
需要 ORDER BY clause
所以提供它是一种满足解析器的方式。
按“常量”排序将创建“不确定”顺序(查询优化器能够选择它认为合适的任何顺序)。
最简单的思考方式是:
ROW_NUMBER() OVER(ORDER BY 1) -- error
ROW_NUMBER() OVER(ORDER BY NULL) -- error
为“欺骗”查询优化器提供常量表达式的可能场景很少:
ROW_NUMBER() OVER(ORDER BY (SELECT 1)) -- already presented
其他选项:
ROW_NUMBER() OVER(ORDER BY 1/0) -- should not be used
ROW_NUMBER() OVER(ORDER BY @@SPID)
ROW_NUMBER() OVER(ORDER BY DB_ID())
ROW_NUMBER() OVER(ORDER BY USER_ID())
我必须在现有查询中添加行号,以便我可以跟踪已将多少数据添加到 Redis。如果我的查询失败,那么我可以从在其他 table 中更新的那一行开始。
查询以从 table
的 1000 行后开始获取数据SELECT * FROM (SELECT *, ROW_NUMBER() OVER (Order by (select 1)) as rn ) as X where rn > 1000
查询工作正常。如果有任何方法我可以在不使用 order by 的情况下获得行号。
这里的select 1
是什么?
是否优化了查询,或者我可以通过其他方式进行优化。请提供更好的解决方案。
只尝试 order by 1
。阅读错误消息。然后恢复order by (select 1)
。意识到写这篇文章的人在某个时候已经阅读了错误消息,然后决定正确的做法是欺骗系统不引发错误,而不是意识到错误试图提醒他们的基本事实。
表格没有固有的顺序。如果您想要某种可以依赖的排序形式,您可以为任何 ORDER BY
子句提供足够的确定性表达式,以便每一行都被唯一标识和排序。
其他任何事情,包括欺骗系统使其不发出错误,都是希望系统在不使用提供给您的工具的情况下做一些明智的事情来确保 它做了一些明智的事情 - 一个明确的 ORDER BY
子句。
您可以使用任何文字值
前
order by (select 0)
order by (select null)
order by (select 'test')
等等
请参阅此以获取更多信息 https://exploresql.com/2017/03/31/row_number-function-with-no-specific-order/
无需担心在 ORDER BY
表达式中指定常量。以下内容引自Itzik Ben-Gan
所著Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions(可从微软免费电子书网站免费下载):
As mentioned, a window order clause is mandatory, and SQL Server doesn’t allow the ordering to be based on a constant—for example, ORDER BY NULL. But surprisingly, when passing an expression based on a subquery that returns a constant—for example, ORDER BY (SELECT NULL)—SQL Server will accept it. At the same time, the optimizer un-nests, or expands, the expression and realizes that the ordering is the same for all rows. Therefore, it removes the ordering requirement from the input data. Here’s a complete query demonstrating this technique:
SELECT actid, tranid, val,
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
FROM dbo.Transactions;
Observe in the properties of the Index Scan iterator that the Ordered property is False, meaning that the iterator is not required to return the data in index key order
上面的意思是当你使用constant ordering时不执行。我强烈建议阅读这本书,因为 Itzik Ben-Gan
深入描述了 window 函数的工作原理以及如何在使用它们时优化各种情况。
What is select 1 here?
在这种情况下,查询的作者并没有真正考虑任何特定的排序。
ROW_NUMBER
需要 ORDER BY clause
所以提供它是一种满足解析器的方式。
按“常量”排序将创建“不确定”顺序(查询优化器能够选择它认为合适的任何顺序)。
最简单的思考方式是:
ROW_NUMBER() OVER(ORDER BY 1) -- error
ROW_NUMBER() OVER(ORDER BY NULL) -- error
为“欺骗”查询优化器提供常量表达式的可能场景很少:
ROW_NUMBER() OVER(ORDER BY (SELECT 1)) -- already presented
其他选项:
ROW_NUMBER() OVER(ORDER BY 1/0) -- should not be used
ROW_NUMBER() OVER(ORDER BY @@SPID)
ROW_NUMBER() OVER(ORDER BY DB_ID())
ROW_NUMBER() OVER(ORDER BY USER_ID())