SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试 [T]
前些天看到一篇文章《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,命名实例
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)
沿用上文的测试数据库和表:
- 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():
- /*
- 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语句如下:
- /*
- 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中执行如下语句:
- 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()方式有了较大的提升。
助人等于自助! 3w@live.cn
SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试 [T]的更多相关文章
- SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试(转)
原文地址:http://www.cnblogs.com/downmoon/archive/2012/04/19/2456451.html 在<SQL Server 2012服务端使用OFFSET ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 2.单表查询
Chapter 2 Single-Table Queries GROUP BY之后的阶段的操作对象就是组(可以把一组想象成很多行组成的)了,HAVING负责过滤掉一些组.分组后的COUNT(*)表示每 ...
- SQL Server 2012 AlwaysOn集群配置指南
1. AlwaysOn介绍 AlwaysOn是SQL Server 2012提供的全新综合.灵活.高效经济的高可用性和灾难恢复解决方案.它整合了镜像和群集的功能,基于OS 故障转移群集(Windows ...
- [转]SQL Server 2012 的 T-SQL 新功能 – 新的数据分析函数(LEAD、LAG)
当您需要在 SQL Server 中利用 T-SQL 比较结果集的每一列跟前一列或后一列的差异时,在过去可能需要利用 CURSOR 搭配临时表变量,或是透过递归 CTE 来达到这个效果,如今 SQL ...
- Microsoft SQL Server 2012安装说明
Microsoft SQL Server 2012安装说明 环境:Windows8, Windows7, WinVista, Win2003, WinXP Microsoft SQL Server 2 ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 4.子查询
Chapter 4 Subqueries 子查询分为:独立子查询(Self-Contained Subqueries)和相关子查询(Correlated Subqueries),独立子查询可以单独拿出 ...
- SQL Server 2012 OFFSET/FETCH NEXT分页示例(转载)
原文:http://beyondrelational.com/modules/29/presentations/483/scripts/12983/sql-server-2012-server-sid ...
- SQL Server 2012 OFFSET/FETCH NEXT分页示例
原文:http://beyondrelational.com/modules/29/presentations/483/scripts/12983/sql-server-2012-server-sid ...
- Sql Server 2012 的新分页方法分析(offset and fetch) - 转载
最近在分析 Sql Server 2012 中 offset and fetch 的新特性,发现 offset and fetch 无论语法的简洁还是功能的强大,都是相当相当不错的 其中 offset ...
随机推荐
- 2017年1月3日 星期二 --出埃及记 Exodus 21:29
2017年1月3日 星期二 --出埃及记 Exodus 21:29 If, however, the bull has had the habit of goring and the owner ha ...
- highchart导出功能的介绍更改exporting源码
本案利用highchar作为前端,展示数据的图形效果,结合spring+springmvc来完成数据图片的导出. jsp引入文件: <script src="${pageContext ...
- 简单利用Scanner对文件进行解析
public class AvPrice{ static int count = 0; static int sum = 0; public static void main(Str ...
- Java-输入输出流
JAVA-输入输出流 1.输入输出: 输入/输出(Input/Output)是指对某个设备或环境进行数据的输入或输出.任何语言都有输入输出的功能,在Java程序中,是通过流来完成输入和输出的,它通过J ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- R中数据拆分和整合
library(data.table)ID <- c(NA,1,2,2)IDTime <- c(1,2,NA,1)TimeX1 <- c(5,3,NA,2)X1X2 <- c( ...
- underscore api
http://files.cnblogs.com/files/hwd13/underscore.rar
- MySQL + Atlas --- 部署读写分离
阅读目录 1. 数据库用户配置 2. 主从数据库连接 3. Atlas配置 4. 读写分离测试 序章 Atlas是360团队弄出来的一套基于MySQL-Proxy基础之上的代理,修改了MySQL-Pr ...
- Log4j源代码学习
了解log4j的源代码来源于项目中一次需求,我们想将系统所有的warn日志统一收集到common-warn.log的日志中去,以便于系统对其进行监控处理.于是模拟自动生成的error配置完成了warn ...
- 实验一 DOS
实验一.DOS实验 一. 实验目的 DOS(Disk Operating System)是一个使用得十分广泛的磁盘操作系统,就连眼下流行的Windows9x/ME系统都是以它为基础 ...