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 ,同时更换硬件,但迁移后 ...
随机推荐
- cocos2d-x CCSrollView 源代码,可循环的SrollView代码
项目须要.写一个类似于iPhone上面时钟选择的可拉动式循环选择列表,通过集成CCScrollView并更改部分代码.实现了该功能. 假设想充分了解代码,请先阅读源码分析http://blog.csd ...
- Hibernate也须要呵护——Hibernate的泛型DAO
众所周之.面向对象的基础是抽象.也能够说,抽象促使编程在不断发展. 对于数据库的訪问,以前写过HqlHelper.EFHelper.编写Spring+Hibernate框架下的应用.也相同离不了编写一 ...
- Servlet学习总结,为理解SpringMVC底层做准备
Servlet 一句话概括 :处理web浏览器,其他HTTP客户端与服务器上数据库或其他应用交互的中间层 Servlet 生命周期 : 1.类加载, 2.实例化并调用init()方法初始化该 Serv ...
- 【iOS系列】-UIWebView加载网页禁止左右滑动
[iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...
- [办公自动化]计算机突然断电,微软office文档(有asd文件)如何恢复?
今天同事使用office软件时,突然故障.结果他忙了半天的word文档内容都找不见了. 经过查找,在其硬盘根目录找到了asd文档. 但是用当前版本的word和高版本的word软件都无法打开. 又查找了 ...
- chrome浏览器世界之窗浏览器的收藏夹在哪?
今天心血来潮,用一个查重软件删除重复文件,结果把chrome浏览器和世界之窗浏览器的收藏夹给删除了,导致我保存的好多网页都没有了,在浏览器本身和网上都没有找到这两个浏览器默认的收藏夹在哪个位置,只好用 ...
- Override is not allowed when implementing interface method Bytecode Version Overriding and Hiding Methods
java - @Override is not allowed when implementing interface method - Stack Overflow https://stackove ...
- win10复制粘贴 失效
win10复制粘贴 DISM.exe /Online /Cleanup-image /Restorehealth https://social.technet.microsoft.com/Forums ...
- SVN服务器端的使用
SVN服务器端的使用 1.下载VirtualSVN Server,安装好后打开,右键Repository->新建->Repository创意一个版本库.默认点击下一步,输入要创建版本库的名 ...
- Local Databases with SQLiteOpenHelper
Overview For maximum control over local data, developers can use SQLite directly by leveraging SQLit ...