簡體   English   中英

在SQL Server中找到第100個第200個等記錄

[英]Finding the 100th 200th and so on records in SQL Server

我在SQL Server中有一個大表,如下所示:

tbl_point

point     time
1         dec 12 12 pm
2         dec 12 12 01 pm
.
. 
100       dec 15 2 pm
. 
.
200       dec 18 5 pm

我需要選擇第100、200、300至n00條記錄

喜歡

100     dec 15 2 pm
200     dec 18 5 pm
.
.
select * from tbl_point where point%100=0 and point>0

1) point%100=0不可SARG。 如果沒有計算列和該計算列上的索引(這需要額外的存儲空間),則該謂詞將強制執行Scan

2)根據[選定索引的葉子頁] / [堆結構的頁]中的點分布,以下解決方案可以提供更好的性能(需要使用真實數據進行測試)。 如果有Index Seek SQL Server可以在point上使用Index Seek (嵌套循環)。

DECLARE @LastPoint INT;
SELECT  TOP(1) @LastPoint = h.Point
FROM    dbo.MyTable h
ORDER BY h.Point DESC;

WITH N(Num)
AS (
    SELECT 1
    UNION ALL 
    SELECT  n.Num + 1
    FROM    N 
    WHERE   n.Num + 1 <= @LastPoint / 100
)
SELECT  h.Point
FROM    N 
INNER /*HASH*/ JOIN dbo.MyTable h ON h.Point = n.Num * 100
OPTION (MAXRECURSION 0);

跟隨索引CREATE INDEX IX_MyTable ON dbo.MyTable(Point) INCLUDE (Time); 對於此解決方案很有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM