SQL Server 2008中SQL應用系列--目錄索引sql
前些天看到一篇文章《SQL Server 2012 - Server side paging demo using OFFSET/FETCH NEXT》,原文地址。做者在文中稱,要SQL Server 2012使用OFFSET/FETCH NEXT分頁,比SQL Server 2005/2008中的RowNumber()有顯著改進。今天特意做了簡單測試。現將過程分享以下:數據庫
附:個人測試環境爲:
SQL Server 2012,命名實例ide
Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86)
Feb 10 2012 19:13:17
Copyright (c) Microsoft Corporation
Enterprise Edition on Windows NT 6.0 <X86> (Build 6002: Service Pack 2)測試
沿用上文的測試數據庫和表:fetch
- IF OBJECT_ID('DemoPager2012') IS NOT NULL
- DROP DataBase DemoPager2012
- GO
-
- CREATE Database DemoPager2012
- GO
-
- USE DemoPager2012
- GO/*
- Setup script to create the sample table and fill it with
- sample data.
- */
- IF OBJECT_ID('Customers','U') IS NOT NULL
- DROP TABLE Customers
-
- CREATE TABLE Customers ( CustomerID INT primary key identity(1,1),
- CustomerNumber CHAR(4),
- CustomerName VARCHAR(50),
- CustomerCity VARCHAR(20) )
- GOTRUNCATE table Customers
- GO
-
- DBCC DROPCLEANBUFFERS
- DBCC FREEPROCCACHE
-
- /*****運用CTE遞歸插入,速度較快,邀月注***********************/
- WITH Seq (num,CustomerNumber, CustomerName, CustomerCity) AS
- (SELECT 1,cast('0000'as CHAR(4)),cast('Customer 0' AS NVARCHAR(50)),cast('X-City' as NVARCHAR(20))
- UNION ALL
- SELECT num + 1,Cast(REPLACE(STR(num, 4), ' ', '0') AS CHAR(4)),
- cast('Customer ' + STR(num,6) AS NVARCHAR(50)),
- cast(CHAR(65 + (num % 26)) + '-City' AS NVARCHAR(20))
- FROM Seq
- WHERE num <= 10000
- )
- INSERT INTO Customers (CustomerNumber, CustomerName, CustomerCity)
- SELECT CustomerNumber, CustomerName, CustomerCity
- FROM Seq
- OPTION (MAXRECURSION 0)
插入1萬條數據後,在SQL Server 2008 R2中執行Row_Number():ui
- /*
- Server side paging demo using ROW_NUMBER() - SQL Server
- 2005/2008 version.
- */
-
- DBCC DROPCLEANBUFFERS
- DBCC FREEPROCCACHE
-
- SET STATISTICS IO ON;
- SET STATISTICS TIME ON;
- GO
-
-
- DECLARE @page INT, @size INT
- SELECT @page = 3, @size = 10
-
- ;WITH cte AS (
- SELECT TOP (@page * @size)
- CustomerID,
- CustomerName,
- CustomerCity,
- ROW_NUMBER() OVER(ORDER BY CustomerName ) AS Seq,
- COUNT(*) OVER(PARTITION BY '') AS Total
- FROM Customers
- WHERE CustomerCity IN ('A-City','B-City')
- ORDER BY CustomerName ASC
- )
- SELECT * FROM cte
- WHERE seq BETWEEN (@page - 1 ) * @size + 1 AND @page * @size
- ORDER BY seq;
- GO
-
- SET STATISTICS IO OFF ;
- SET STATISTICS TIME OFF;
- GO
SQL Server 2012中執行OFFSET/FETCH NEXT語句以下:spa
- /*
- Server side paging demo using the new enhancements added
- in SQL Server 2012
- */
- DBCC DROPCLEANBUFFERS
- DBCC FREEPROCCACHE
-
- SET STATISTICS IO ON;
- SET STATISTICS TIME ON;
- GO
-
-
- DECLARE @page INT, @size INT
- SELECT @page = 3, @size = 10
-
- SELECT
- *,
- COUNT(*) OVER(PARTITION BY '') AS Total
- FROM Customers
- WHERE CustomerCity IN ('A-City','B-City')
- ORDER BY CustomerID
- OFFSET (@page -1) * @size ROWS
- FETCH NEXT @size ROWS ONLY;
- GO
-
- SET STATISTICS IO OFF;
- SET STATISTICS TIME OFF;
- GO
在SQL Server 2012中執行以下語句:.net
- DBCC DROPCLEANBUFFERS
- DBCC FREEPROCCACHE
-
- SET STATISTICS IO ON;
- SET STATISTICS TIME ON;
- GO
-
- DECLARE @page INT, @size INT
- SELECT @page = 3, @size = 10
-
- ;WITH cte AS (
- SELECT TOP (@page * @size)
- CustomerID,
- CustomerName,
- CustomerCity,
- ROW_NUMBER() OVER(ORDER BY CustomerName ) AS Seq,
- COUNT(*) OVER(PARTITION BY '') AS Total
- FROM Customers
- WHERE CustomerCity IN ('A-City','B-City')
- ORDER BY CustomerName ASC
- )
- SELECT * FROM cte
- WHERE seq BETWEEN (@page - 1 ) * @size + 1 AND @page * @size
- ORDER BY seq;
-
-
- SELECT
- *,
- COUNT(*) OVER(PARTITION BY '') AS Total
- FROM Customers
- WHERE CustomerCity IN ('A-City','B-City')
- ORDER BY CustomerID
- OFFSET (@page -1) * @size ROWS
- FETCH NEXT @size ROWS ONLY;
- GO
-
- SET STATISTICS IO OFF;
- SET STATISTICS TIME OFF;
- GO
結論:不管是從邏輯讀取數仍是響應時間、實際執行行數等關鍵參數看,SQL Server 2012提供的OFFSET/FETCH NEXT分頁方式都比Row_Number()方式有了較大的提高。server
邀月注:本文版權由邀月和CSDN共同全部,轉載請註明出處。助人等於自助! 3w@live.cn