分页实现方法的性能比较
来源:CSDN 发布时间:2007-06-24 12:41:00
我们先给出几种主要的分页方法和核心语句,然后直接给出结论,有兴趣的读者可以看看后面的数据
几种常用存储过程分页方法
TopN方法
select Top(@PageSize) from TableName where ID Not IN
(Select Top ((@PageIndex-1)*@PageSize) ID from Table Name where .... order by ... )
where .... order by ...
临时表
declare @indextable table(id int identity(1,1),nid int,PostUserName nvarchar(50))
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize--下限
set @PageUpperBound=@PageLowerBound+@pagesize--上限
set rowcount @PageUpperBound
insert into @indextable(nid,PostUserName) select ReplyID,PostUserName from TableName order by ......
select * from TableName p,@indextable t where p.ID=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
CTE--2005新语法,类似临时表,但是生命周期稍微不同,这里只是他的一个运用
with cte_temp--定义零时表,PageIndex是一个计算字段,储存了搜索结果的页号
As (ceiling((Row_Number() over(order by .... )-1)/@pagesize as int) as PageIndex,* from TableName where.....)
select * from cte_temp where pageindex=@pageindex-1;
结论:
TopN在小页数下最快,如果在10页以下,可以考虑用它,CTE和临时表时间很稳定,CTE消耗的时间比临时表多,但是不会引起tempdb的暴涨和IO增加
性能比较
试验环境:win2003server,Sqlserver2005,库大小2,567,245行,没有where子句,试验时每页大小50,页码作为变量
取0,3,10,31,100,316,1000,3162...页,也就是10的指数,试验结果如下
页数 TopN CTE 临时表(有缓存) 临时表(无缓存)
公司正在使用的存储过程 CTE改进
1 3 12 10 101 457 7302
3 15 7 79 5524 464 7191
10 127 5504 88 3801 464 6116
32 588 9672 122 3601 976 7602
100 4680 9738 166 4235 486 7151
316 45271 9764 323 3867 522 7255
1000 Null 9806 869 2578 635 8948
3162 Null 9822 2485 4110 12460 8210
10000 Null 9754 7812 11926 14250 7359
31623 Null 9775 18729 33218 15249 7511
100000 Null Null 31538 55569 17139 6124
数据解释和分析
临时表分为有没有缓存两种时间,CTE就是上面的方法,CTE改进只是把选入CTE临时表的列数减少了,只选取了页号和主键,Null表示时间无法计算(时间太长),数据单位是毫秒.
从上面的数据可以看到,TopN在前32页都是有优势的,但是页数增大后,性能降低很快,CTE改进比CTE有所进步,平均进步两秒左右,但是还是比临时表慢,但是考虑临时表会增大日志文件的大小,引起大量IO,CTE也就有他自己的优势,公司现在正在使用的存储过程效率不错,但是在页码靠后的情况下性能会降低
猜你喜欢
- 对于rsync配置,在google上可以找到很多资料,以下只是一些必要的配置和使用说明,仅供参考,请根据您的实际情况修改。如图所示,需要在同
- 1、博客链接有用么?解答:博客链接的作用是有的,但是要给blog常更新内容,现在很多blog都用ajax,意义不大。csdn 挺不错的,会被
- 域名:GoDaddy是世界第一大域名注册商,域名的附加服务很多,有blog,Email,email指向,停放页,出售页面等。而且可以很方便的
- 无论是对普通网络冲浪者还是网站管理员来说,Google都是目前世界范围内最受欢迎的搜索引擎。它每天处理的搜索请求高达1.5亿次,几乎占全球所
- 和大多数人一样,我是一个站长,一个落魄的个人站长,终日在城市里颠沛流离,在网络上寻求温饱。不经意间,QQ已经在10年的成长之路上记录了中国网
- 据国外媒体报道,激进主义投资者卡尔·伊坎(CarlIcahn)周五宣布,他已经从雅虎董事会辞职。伊坎在一份声明中称:“雅虎此时已经不再需要一
- 本文假定读者已经理解双机互备的HA方案的基本概念。1. 双机互备HA方案的基本步骤建立一个双机互备方案的基本步骤是:1. 确定基本参数(如I
- 最新消息,康盛创想(Comsenz)官方将推出Discuz! 7.1版本,该版本在继承Discuz! 7.0版本经典基础之上进一步创新和完善
- Ubuntu apt-get指令下面总结一下有关apt-get的常用但容易混淆的指令:apt-get autoclean: &nb
- 易观国际(Analysys International)的分析认为,随着网络购物逐步升级为中国消费者的主流消费渠道,中国网上零售市场未来最大
- 各种不同网站运营模式是不同的就比如说阿里巴巴网站运营模式和开心网网站运营模式[开心网运营的盈利模式] ,是截然不同的,那再想下,是什么决定了
- Outlook Web Access(OWA)让拥有Exchange帐户的用户能够从任何一台有网络浏览器的电脑上管理他们的电子邮件,但不是所
- 好的开始是成功过的一半,个人站长的队伍越来越庞大,找不到方向的站长也是越来越多,个人站长的发展方向到底在哪里。到底怎么做才能通过网站赚钱,今
- 大家先来看看问题描述:新建虚拟机的列表中没有64位系统选项,如何解决?操作系统:Windows 7 64位;虚拟机:Oracle VM Vi
- 最近观察了几个刚做的网站,都是医疗行业的,百度半个月左右才收录,这几个医疗的网站到现在的收录还是停止不前,日期也是如此,每天照旧跟新,为什么
- 说句实在话,作为中国排名第一的搜索引擎,出现这种情况真的是很不应该,但是却着实出现了。在五月的时候,很多的seoer就讨论过这个现象。有很多
- Linux启动后出现boot:提示时,使用一个特殊的命令,如linuxsingle或linux 1,就能进入单用户模式(Single-Use
- 北京时间10月14日消息,据国外媒体报道,Google近两年来遭遇了高管离职潮,仅在今年9月就有两位高管先后离开。业内人士认为,员工离职的原
- 一、基本知识: Sendmail是在Unix环境下使用最广泛的邮件传输代理程序,Sendmail邮件服务器的特点是功能强大但配置复
- 404页面,也称为浏览器在访问时找不到请求的文件或者目录页面,通常是显示一个错误提示页面,但在Linux环境下,可以自定义404页面的显示效