SQLServer 2012 高效分页
SQLSERVER2012 出新分页功能啦!!!
近两天我在自己工作机的PC(没有并发,单一线程)上做了SqlServer 2000/ (2005/2008)/2012三个版本下的分页性能比较。
大致可得出以下结果:
1、表数据量200W以内:SQLServer2012 的offset/fetch分页性能和SQLServer2005 Row_number的分页性能(仅考虑出结果速度)基本没区别(难分高下),略高于(大约10%)SQL2000的TOP分页性能。
2、表数据量2000W左右:SQLServer2012 的offset/fetch分页性能略高于SQLServer2005 Row_number的分页性能,主要体现在IO上,但是两者性能可算是远高于(大约25%)SQL2000的TOP分页性能。
3、执行计划2012比2005简单,2005比2000简单,学习简易程度,2012最容易实现。
特此分享一下,下面是我的测试脚本,有兴趣可以自己也试试
测试环境:
Microsoft SQL Server 2014 - 12.0.2000.8 (X64)
Feb 20 2014 20:04:26
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
sql code
/*
功能:生成测试数据.
*/ create table Test_paging(
id int identity(1,1) not null primary key,
TestNumber int not null,
TestName varchar(20) not null,
TestDept varchar(10) not null,
TestDate datetime not null
)
go with tep(Number,Name,Dept,Date) as
(
select 1,cast('0_testname' as varchar(20)),cast('0_DBA' as varchar(10)),getdate()
union all
select Number+1,cast(cast(Number as varchar(20))+'_testname' as varchar(20)),cast(cast(Number/500 as varchar(10))+'_DBA' as varchar(10)) ,getdate()
from tep
where Number<=20000000
)
insert into Test_paging(TestNumber,TestName,TestDept,TestDate)
select Number,Name,Dept,Date from tep option(maxrecursion 0) --添加索引(我有测试没有索引的情况,2012的优势更加明显,但是我们的数据库不可能不建索引的,故可忽略没有索引的情况)
create nonclustered index IX_TestDept on Test_paging(
TestDept
) include
(
TestName,TestDate
)
go
SQL code
/*
功能:测试2012版本中offset/fetch分页.
*/ dbcc dropcleanbuffers
dbcc freeproccache set statistics io on
set statistics time on
set statistics profile on declare
@page int, --第@page页
@size int, --每页@size行
@total int --总行数 select @page=20,@size=10,@total=count(1) from Test_paging where TestDept = '1000_DBA' select
TestName,TestDept,TestDate,@total
from
Test_paging
where
TestDept = '1000_DBA'
order by id offset (@page-1)*@size rows fetch next @size rows only set statistics io off
set statistics time off
set statistics profile off
SQL code
/*
功能:测试2005/2008版本中row_number分页.
*/ dbcc dropcleanbuffers
dbcc freeproccache set statistics io on
set statistics time on
set statistics profile on declare
@page int, --第@page页
@size int, --每页@size行
@total int select @page=20,@size=10,@total=count(1) from Test_paging where TestDept = '1000_DBA' select TestName,TestDept,TestDate,@total from
(
select
TestName,TestDept,TestDate,row_number() over(order by ID) as num
from
Test_paging
where
TestDept = '1000_DBA'
) test where num between (@page-1)*@size+1 and @page*@size order by num set statistics io off
set statistics time off
set statistics profile off
SQL code
/*
功能:测试2000版本中top分页.
*/ dbcc dropcleanbuffers
dbcc freeproccache set statistics io on
set statistics time on
set statistics profile on declare
@page int, --第@page页
@size int, --每页@size行
@total int --总行数 select @page=20,@size=10,@total=count(1) from Test_paging where TestDept = '1000_DBA' select TestName,TestDept,TestDate,@total from
(
select top(@size) id,TestName,TestDept,TestDate from
(
select top(@page*@size) id,TestName,TestDept,TestDate
from Test_paging
where TestDept = '1000_DBA'
order by id
)temp1 order by id desc
)temp2 order by id set statistics io off
set statistics time off
set statistics profile off
原文:http://bbs.csdn.net/topics/390941777
SQLServer 2012 高效分页的更多相关文章
- T-SQL 使用WITH高效分页
一.WITH AS 含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...
- EFCore中SQLSERVER 2008 的分页问题
自SQLSERVER 2012起新增了 Offset Fetch 语法,因此EFCore默认是以此语法生成相应的分页语句的. 如果我们的目标数据库低于 2012,那么EFCore默认生成的语句在执行的 ...
- SQLSERVER 2012计算上一条,下一条数据的函数
实际需求很普遍,比如求销售数据的每天与头一天的销售增长量.这里用一个汽车行驶数据来做例子: 先初始化数据: CREATE TABLE [dbo].[CarData]( [CarID] [int] NU ...
- SQLSERVER 2012之AlwaysOn -- 一次硬件升级引发的问题
这是上周遇到的一个案例:对已有的硬件进行升级而引发的问题,期间还触发了一个比较严重的BUG,可谓多灾多难:不过值得庆幸的是,在一连串连锁问题出现的时候,并没有出现人工操作失误(这往往是在处理故障中风险 ...
- SQLSERVER 2012之AlwaysOn -- 同步模式下的网卡性能优化
本文是基于上一篇<SQLServer 2012之AlwaysOn -- 指定数据同步链路,消除网络抖动导致的提交延迟问题>的问题继续进行优化:具体背景请参照上文: 前后折腾了一个多 ...
- SQL 2012的分页
今天看到一篇文章介绍2012中的分页,就想测试一下新的分页方法比原先的有多少性能的提升,下面是我的测试过程(2012的分页语法这里不在做多的说明,MSDN上一搜就有): 首先我们来构造测试数据: -- ...
- SQLServer 2012 可视化窗口中,设置“时间”默认值为“当前时间"
最近,需要在SQLServer 2012中,设置datetime的默认值为当前时间. 通过可视化窗口进行设置,而不是将getdate()函数写在sql语句中,也不是将‘2022-2-2 22:22:2 ...
- C#高效分页代码(不用存储过程)
首先创建一张表(要求ID自动编号): create table redheadedfile ( id ,), filenames ), senduser ), primary key(id) ) 然后 ...
- SQLServer 2012异常问题(二)--由安装介质引发性能问题
原文:SQLServer 2012异常问题(二)--由安装介质引发性能问题 问题描述:生产环境一个数据库从SQLSERVER 2008 R2升级到SQLSERVER 2012 ,同时更换硬件,但迁移后 ...
随机推荐
- python各进制、字节串间的转换
>>> i = 13 >>> bin(i) '0b1101' >>> oct(i) '0o15' >>> hex(i) '0xd ...
- mac上为nginx打开防火墙
1 nginx的路径必须是物理路径,不能是链接 2 执行下面的两个命令后重启电脑 命令 sudo /usr/libexec/ApplicationFirewall/socketfilterfw --a ...
- select case when if
select case when if 的一些用法 - 马丁传奇 - 博客园 https://www.cnblogs.com/martinzhang/p/3220595.html Write a SQ ...
- 《31天成为IT服务达人》--做事篇(第四章)之如何找目标
前面介绍了什么是IT服务.以下几章将介绍IT服务该怎么做.在聊怎么做之前.想起几句流行的告白和准备入行IT服务事业的朋友共勉. 当你的才华 还撑不起你的野心时 就应该静下心来 学习 --- 当你 ...
- 搭建基于Maven的SSM框架
先展示文件结构图对工程结构有大致了解: 主要为 ssm-parent (用来管理jar包版本)是每个工程的父工程,ssm-common(用来处理底层数据),ssm-manager(对数据库信息进行操 ...
- URAL1099 Work Scheduling —— 一般图匹配带花树
题目链接:https://vjudge.net/problem/URAL-1099 1099. Work Scheduling Time limit: 0.5 secondMemory limit: ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- Newtonsoft.Json.dll 反序列化JSON字符串
上一篇JSON博客<JSON入门级学习小结--JSON数据结构>中已对JSON做了简单介绍,JSON字符串数组数据样式大概是这样子的: 如今因为项目需求(asp.net web网站,前台向 ...
- 推荐使用集串口,SSH远程登录和FTP传输三合一工具MobaXterm
在以前的资料里,串口和SSH远程登使用SecureCRT,window与ubuntu数据传输使用filezilla,窗口切换来切换去,麻烦也眼花缭乱.有没有一个工具搞定串口.SSH和FTP?有!它就是 ...
- 详解Python的*args和 **kwargs
转自: http://www.python[tab].com/html/2016/pythonhexinbiancheng_0802/1057.html *args表示任何多个无名参数,它是一个tup ...