对于它的价值,这里有一些 SQLFiddle来测试任何语法。
到目前为止,最简单的方法是确保NameTypes
按顺序插入s Name
,因此NameTypeID
按字母顺序分配 s。
在这种情况下,无需加入NameTypes
表。你可以这样做,
WITH [CTE] AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY [NameTypeID] ASC) [PetaRN],
[DocumentNameID],
[DocumentID]
FROM
[DocumentNames]
)
SELECT
[PetaRN],
[DocumentNameID],
[DocumentID]
FROM
[CTE]
WHERE
[PetaRN] BETWEEN 10000 AND 10050
ORDER BY
[PetaRN] ASC;
怎么样
WITH [CTE] AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY [NameTypeID] ASC) [PetaRN],
[DocumentNameID],
[DocumentID]
FROM
[DocumentNames]
)
SELECT TOP 50
[PetaRN],
[DocumentNameID],
[DocumentID]
FROM
[CTE]
WHERE
[PetaRN] >= 10000
ORDER BY
[PetaRN] ASC;
在 SQL 2005 上测试大型数据集时,我注意到 CTE 对于大型结果集表现不佳,这可能与服务器上的资源可用性有关。临时表的反直觉使用可能会更快。这也允许您索引行号以允许快速页面选择,但是,这必须抵消插入成本。试试看。
CREATE TABLE #Peta
(
[PetaRN] BigInt NOT NULL CONSTRAINT [PK_Peta] PRIMARY KEY CLUSTERED,
[DocumentNameID] Int NOT NULL,
[DocumentID] Int NOT NULL
);
INSERT #Peta
SELECT
ROW_NUMBER() OVER (ORDER BY [NameTypeID] ASC) [PetaRN],
[DocumentNameID],
[DocumentID]
FROM
[DocumentNames];
SELECT TOP 50
[PetaRN],
[DocumentNameID],
[DocumentID]
FROM
#Peta
WHERE
[PetaRN] >= 10000
ORDER BY
[PetaRN] ASC;
DROP TABLE #Peta;