架构师_程序员

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 264|回复: 0

[交流] SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试

[复制链接]
跳转到指定楼层
楼主
发表于 2019-5-7 10:35:50
zu
SQL Server 2012使用OFFSET/FETCH NEXT分页,比SQL Server 2005/2008中的RowNumber()有显著改进。今天特地作了简单测试。现将过程分享如下:

附:我的测试环境为:

SQL Server 2012,命名实例

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)

沿用上文的测试数据库和表:

  1. IF OBJECT_ID('DemoPager2012') IS NOT NULL
  2. DROP DataBase DemoPager2012
  3. GO

  4. CREATE Database DemoPager2012
  5. GO

  6. USE DemoPager2012
  7. GO/*
  8. Setup script to create the sample table and fill it with
  9. sample data.
  10. */
  11. IF OBJECT_ID('Customers','U') IS NOT NULL
  12. DROP TABLE Customers

  13. CREATE TABLE Customers ( CustomerID INT primary key identity(1,1),
  14. CustomerNumber CHAR(4),
  15. CustomerName VARCHAR(50),
  16. CustomerCity VARCHAR(20) )
  17. GOTRUNCATE table Customers
  18. GO

  19. DBCC DROPCLEANBUFFERS
  20. DBCC FREEPROCCACHE

  21. /*****运用CTE递归插入,速度较快,邀月注***********************/
  22. WITH Seq (num,CustomerNumber, CustomerName, CustomerCity) AS
  23. (SELECT 1,cast('0000'as CHAR(4)),cast('Customer 0' AS NVARCHAR(50)),cast('X-City' as NVARCHAR(20))
  24. UNION ALL
  25. SELECT num + 1,Cast(REPLACE(STR(num, 4), ' ', '0') AS CHAR(4)),
  26. cast('Customer ' + STR(num,6) AS NVARCHAR(50)),
  27. cast(CHAR(65 + (num % 26)) + '-City' AS NVARCHAR(20))
  28. FROM Seq
  29. WHERE num <= 10000
  30. )
  31. INSERT INTO Customers (CustomerNumber, CustomerName, CustomerCity)
  32. SELECT CustomerNumber, CustomerName, CustomerCity
  33. FROM Seq
  34. OPTION (MAXRECURSION 0)
复制代码
插入1万条数据后,在SQL Server 2008 R2中执行Row_Number():
  1. /*
  2. Server side paging demo using ROW_NUMBER() - SQL Server
  3. 2005/2008 version.
  4. */

  5. DBCC DROPCLEANBUFFERS
  6. DBCC FREEPROCCACHE

  7. SET STATISTICS IO ON;
  8. SET STATISTICS TIME ON;
  9. GO


  10. DECLARE @page INT, @size INT
  11. SELECT @page = 3, @size = 10

  12. ;WITH cte AS (
  13. SELECT TOP (@page * @size)
  14. CustomerID,
  15. CustomerName,
  16. CustomerCity,
  17. ROW_NUMBER() OVER(ORDER BY CustomerName ) AS Seq,
  18. COUNT(*) OVER(PARTITION BY '') AS Total
  19. FROM Customers
  20. WHERE CustomerCity IN ('A-City','B-City')
  21. ORDER BY CustomerName ASC
  22. )
  23. SELECT * FROM cte
  24. WHERE seq BETWEEN (@page - 1 ) * @size + 1 AND @page * @size
  25. ORDER BY seq;
  26. GO

  27. SET STATISTICS IO OFF ;
  28. SET STATISTICS TIME OFF;
  29. GO
复制代码


SQL Server 2012中执行OFFSET/FETCH NEXT语句如下:

  1. /*
  2. Server side paging demo using the new enhancements added
  3. in SQL Server 2012
  4. */
  5. DBCC DROPCLEANBUFFERS
  6. DBCC FREEPROCCACHE

  7. SET STATISTICS IO ON;
  8. SET STATISTICS TIME ON;
  9. GO


  10. DECLARE @page INT, @size INT
  11. SELECT @page = 3, @size = 10

  12. SELECT
  13. *,
  14. COUNT(*) OVER(PARTITION BY '') AS Total
  15. FROM Customers
  16. WHERE CustomerCity IN ('A-City','B-City')
  17. ORDER BY CustomerID
  18. OFFSET (@page -1) * @size ROWS
  19. FETCH NEXT @size ROWS ONLY;
  20. GO

  21. SET STATISTICS IO OFF;
  22. SET STATISTICS TIME OFF;
  23. GO
复制代码


在SQL Server 2012中执行如下语句:

  1. DBCC DROPCLEANBUFFERS
  2. DBCC FREEPROCCACHE

  3. SET STATISTICS IO ON;
  4. SET STATISTICS TIME ON;
  5. GO

  6. DECLARE @page INT, @size INT
  7. SELECT @page = 3, @size = 10

  8. ;WITH cte AS (
  9. SELECT TOP (@page * @size)
  10. CustomerID,
  11. CustomerName,
  12. CustomerCity,
  13. ROW_NUMBER() OVER(ORDER BY CustomerName ) AS Seq,
  14. COUNT(*) OVER(PARTITION BY '') AS Total
  15. FROM Customers
  16. WHERE CustomerCity IN ('A-City','B-City')
  17. ORDER BY CustomerName ASC
  18. )
  19. SELECT * FROM cte
  20. WHERE seq BETWEEN (@page - 1 ) * @size + 1 AND @page * @size
  21. ORDER BY seq;


  22. SELECT
  23. *,
  24. COUNT(*) OVER(PARTITION BY '') AS Total
  25. FROM Customers
  26. WHERE CustomerCity IN ('A-City','B-City')
  27. ORDER BY CustomerID
  28. OFFSET (@page -1) * @size ROWS
  29. FETCH NEXT @size ROWS ONLY;
  30. GO

  31. SET STATISTICS IO OFF;
  32. SET STATISTICS TIME OFF;
  33. GO
复制代码


结论:无论是从逻辑读取数还是响应时间、实际执行行数等关键参数看,SQL Server 2012提供的OFFSET/FETCH NEXT分页方式都比Row_Number()方式有了较大的提升。





上一篇:[翻译] 微软正式宣布 .NET 5版本
下一篇:vscode搜索图标不见了
帖子永久地址: 

架构师_程序员 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与架构师_程序员享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和架构师_程序员的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、架构师_程序员管理员和版主有权不事先通知发贴者而删除本文

码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。

Mail To:help@itsvse.com

QQ|Archiver|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2019-7-18 03:26

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表