SQL查询:如何直接获取第1000行的数据?
如果我有一个名为 Persons
的 SQL 表,其中包含大约 30000 行,并且我想创建一个 SQL 查询来检索行号 1000 的数据...我通过非专业方式通过以下方式获得了它查询
Select Top 1 * from
(
Select top 1000 *
From Persons
Order By ID
)A
Order By A.ID desc
但我觉得这是一个更优化的查询,可以做到这一点......任何可以引导我完美的查询吗?
注意:表包含名为“ID”的 PK 列,但它不是连续的
If I have a SQL Table called Persons
that contain about 30000 rows and I want to make a SQL query that retrieve the data of row number 1000 ... I got it by non professional way by making the following query
Select Top 1 * from
(
Select top 1000 *
From Persons
Order By ID
)A
Order By A.ID desc
But I feel that's a more optimized query that can do that ... can any lead me to perfect query ?
Note : table contain PK column called "ID" but it's not sequential
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
row_number
是最好的方法,但由于您只需要一行,因此请务必查看计划。识别所需的行然后连接回原始表以检索其他列可能会更好。给予
给予
row_number
is the best approach but as you only want a single row be sure to look at the plan. It might turn out better to identify the desired row then join back onto the original table to retrieve additional columns.Gives
Gives
在 SQL Server 2005+ 中,您可以使用以下命令:
In SQL Server 2005+ you can use the following:
你可以试试这个
you can try this