簡體   English   中英

在SQL Server 2000中分頁大量數據的最有效方法是什么?

[英]What is the most efficient way to page large amounts of data in SQL Server 2000?

如果我有一個包含大量信息的查詢(例如幾個視圖,每個視圖都命中了幾個表,許多表有成千上萬的行),而我只需要從中獲取10條記錄即可顯示給用戶,在性能方面,最好的方法是在仍支持SQL Server 2000的情況下檢索這些記錄? 一旦可以使用SQL Server 2005, ROW_NUMBER似乎是顯而易見的選擇(如果我輸入錯了,請糾正我),但是在2000年該怎么辦?

格雷格·漢密爾頓(Greg Hamilton)的文章使用SET ROWCOUNTSELECT到變量中,以避免不得不引用不需要的行,從而獲得了一些令人信服的性能結果。 但是, MSDN說

如果在選擇列表中引用了變量,則應為其分配標量值,否則SELECT語句應僅返回一行。

但是接着說

請注意,只有在分配之間存在引用時,效果才可見。

如果SELECT語句返回多個行,並且該變量引用一個非標量表達式,則該變量將設置為結果集最后一行中該表達式返回的值。

表示在這種情況下真的還可以(對嗎?)

格雷格最終得出以下結論:

CREATE  PROCEDURE [dbo].[usp_PageResults_NAI] 
(
    @startRowIndex int,
    @maximumRows int
)
AS

DECLARE @first_id int, @startRow int

-- A check can be added to make sure @startRowIndex isn't > count(1)
-- from employees before doing any actual work unless it is guaranteed
-- the caller won't do that

-- Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid

-- Now, set the row count to MaximumRows and get
-- all records >= @first_id
SET ROWCOUNT @maximumRows

SELECT e.*, d.name as DepartmentName 
FROM employees e
   INNER JOIN Departments D ON
       e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID

SET ROWCOUNT 0

GO 

此方法假定您具有唯一的ID排序,我認為在對非唯一的DateTime列進行排序時,不能按原樣使用此方法。

暫無
暫無

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

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