SQL查询:如何直接获取第1000行的数据?

发布于 2024-10-26 23:41:08 字数 321 浏览 5 评论 0原文

如果我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐偏执 2024-11-02 23:41:08

row_number 是最好的方法,但由于您只需要一行,因此请务必查看计划。识别所需的行然后连接回原始表以检索其他列可能会更好。

WITH T1
     AS (SELECT *,
                ROW_NUMBER() OVER (ORDER BY number) AS RN
         FROM   master..spt_values)
SELECT name,
       number,
       type,
       low,
       high,
       status
FROM   T1
WHERE  RN = 1000;

给予

表“spt_values”。扫描计数 1,逻辑读取 2005

CPU 时间 = 0 毫秒,运行时间 = 19 毫秒。

计划 1

WITH T2
     AS (SELECT number,
                type,
                name,
                ROW_NUMBER() OVER (ORDER BY number) AS RN
         FROM   master..spt_values)
SELECT TOP 1 C.name,
             C.number,
             C.type,
             C.low,
             C.high,
             C.status
FROM   T2
       CROSS APPLY (SELECT *
                    FROM   master..spt_values v
                    WHERE  v.number = T2.number
                           AND v.type = T2.type
                           AND ( v.name = T2.name
                                  OR ( v.name IS NULL
                                       AND T2.name IS NULL ) )) C
WHERE  RN = 1000;  

给予

表“spt_values”。扫描计数1,逻辑读取7

CPU 时间 = 0 毫秒,运行时间 = 1 毫秒。

计划 2

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.

WITH T1
     AS (SELECT *,
                ROW_NUMBER() OVER (ORDER BY number) AS RN
         FROM   master..spt_values)
SELECT name,
       number,
       type,
       low,
       high,
       status
FROM   T1
WHERE  RN = 1000;

Gives

Table 'spt_values'. Scan count 1, logical reads 2005

CPU time = 0 ms, elapsed time = 19 ms.

plan 1

WITH T2
     AS (SELECT number,
                type,
                name,
                ROW_NUMBER() OVER (ORDER BY number) AS RN
         FROM   master..spt_values)
SELECT TOP 1 C.name,
             C.number,
             C.type,
             C.low,
             C.high,
             C.status
FROM   T2
       CROSS APPLY (SELECT *
                    FROM   master..spt_values v
                    WHERE  v.number = T2.number
                           AND v.type = T2.type
                           AND ( v.name = T2.name
                                  OR ( v.name IS NULL
                                       AND T2.name IS NULL ) )) C
WHERE  RN = 1000;  

Gives

Table 'spt_values'. Scan count 1, logical reads 7

CPU time = 0 ms, elapsed time = 1 ms.

Plan 2

许久 2024-11-02 23:41:08

在 SQL Server 2005+ 中,您可以使用以下命令:

WITH MyCte AS 
(
    SELECT
        [CategoryId]
        ,[CategoryName]
        ,[CategoryDescription]
        ,ROW_NUMBER() OVER (ORDER BY CategoryId ASC) AS RowNum
    FROM
        [Carmack].[dbo].[job_Categories]
)
SELECT *
FROM    MyCte
WHERE   RowNum = 3

In SQL Server 2005+ you can use the following:

WITH MyCte AS 
(
    SELECT
        [CategoryId]
        ,[CategoryName]
        ,[CategoryDescription]
        ,ROW_NUMBER() OVER (ORDER BY CategoryId ASC) AS RowNum
    FROM
        [Carmack].[dbo].[job_Categories]
)
SELECT *
FROM    MyCte
WHERE   RowNum = 3
南烟 2024-11-02 23:41:08

你可以试试这个

select * from Person P
where 999 = ( select count(id) from Person P1 , Person P2 
              where P1.id = P.id and P2.id < P1.id)

you can try this

select * from Person P
where 999 = ( select count(id) from Person P1 , Person P2 
              where P1.id = P.id and P2.id < P1.id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文