我寫了下面的代碼,它工作正常,但如果表中包含一百萬條記錄,則需要3秒才能完成。有沒有辦法來優化下面的代碼。T-sql:如何執行優化的尋呼?
DBCC DROPCLEANBUFFERS;
DBCC FREEPROCCACHE;
DECLARE @Page_Size int;
DECLARE @Page_Number int;
DECLARE @Lower_Bound int;
DECLARE @Upper_Bound int;
SET @Page_Size = 30;
SET @Page_Number = 30000;
SET @Lower_Bound = (@Page_Number - 1) * @Page_Size;
--SET @Upper_Bound = @Page_Number * @Page_Size;
WITH Customers AS--(Row_Numbr, Record_Id, First_Name,
Middle_Name, Last_Name, Email, Telephone) AS
(
SELECT ROW_NUMBER()
OVER
(ORDER BY Account.Customer.Record_Id) AS Row_Numbr, *
FROM Account.Customer
)
SELECT top(@Page_Size) *
FROM Customers
WHERE Row_Numbr > @Lower_Bound--
AND Row_Numbr <= @Upper_Bound -- This is suppose to be faster
--SELECT * FROM Customers
--WHERE Row_Numbr > @Lower_Bound
-- AND Row_Numbr <= @Upper_Bound
執行計劃說什麼? – super9 2010-02-06 18:56:33
你有什麼指標?你能發佈創建表腳本嗎? – 2010-02-06 19:08:37
相關:http://stackoverflow.com/questions/1897436/row-number-over-not-fast-enough-with-large-result-set-any-good-solution – 2010-02-06 20:28:03