免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1105 | 回复: 0
打印 上一主题 下一主题

[RAID与磁盘阵列] 关于存储过程分页 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-27 10:50 |只看该作者 |倒序浏览

看了几个朋友写的关于存储过程分页的文章,感觉有点问题。在此我发表点看法。
首先是allsky的那个分页方法根本就不成立,是这样的:
select @iStart=(@iPage-1)*@iPageSize
select @iEnd=@iStart+@iPageSize+1
也就是说,他的开始和结束id按照每页显示数硬算出来的,想要这种方法成立必须满足这样一个条件,即这个论坛只有一个版面,并且id从1开始是连续的,中间不能有间隔,也就是说如果删贴了的话那就会出错。
其次是starleee那个,其实思路是对的,但既然用求首尾id的方法分页,就没有必要用游标,可以利用select top *或set rowcount = 的语法来求出首尾id,第一种方法只能用在sql server里,而后一种在sybase和oracle里都能成立。
starleee提到说试验过这种方法不如用游标快,其实问题出在他的索引建的不好,没有专门为这个存储过程建立索引。影响数据库效率最大的因素就是索引,在这里有必要讲一下。理论上如果一个排序的第一个字段的索引不能过滤掉大部分数据,那么这个索引就是不恰当的,这样将可能有些晦涩,据个例子来说吧:
select id , name , forumid from tablexxx where forumid=1 and name like ‘%aaa%‘ order by id
看看上边这条语句,如果想要高效,就需要为它建立这样一个索引:
forumid , id
这样说把,如果在一个有百万条纪录的表中用这条语句,如果不建这个索引,最大的可能是要超时,而建立上述索引,如果有满足条件的纪录的话,那可以在1秒钟内响应(选出第一条符合条件的纪录),而如果没有满足条件的纪录,也可以在一分钟内响应。
下面这个存储过程是我的bbs利用求首尾id的方法分页的,大家可以看一下
/*************************************************************************/
/* */
/* procedure : up_GetTopicList */
/* */
/* Description: 贴子列表 */
/* */
/* Parameters: @a_intForumID : 版面id */
/* @a_intPageNo: 页号 */
/* @a_intPageSize: 每页显示数,以根贴为准 */
/* */
/* Use table: bbs , forum */
/* */
/* Author:
bigeagle@163.net
*/
/* */
/* Date: 2000/2/14 */
/* */
/* History: */
/* */
/*************************************************************************/
if exists(select * from sysobjects where id = object_id(‘up_GetTopicList‘))
drop proc up_GetTopicList
go
create proc up_GetTopicList
@a_intForumID int ,
@a_intPageNo int ,
@a_intPageSize int
as
/*定义局部变量*/
declare @intBeginID int
declare @intEndID int
declare @intRootRecordCount int
declare @intPageCount int
declare @intRowCount int
/*关闭计数*/
set nocount on
/*检测是否有这个版面*/
if not exists(select * from forum where id = @a_intForumID)
return (-1)
/*求总共根贴数*/
select @intRootRecordCount = count(*) from bbs where fatherid=0 and forumid=@a_intForumID
if (@intRootRecordCount = 0) --如果没有贴子,则返回零
return 0
/*判断页数是否正确*/
if (@a_intPageNo - 1) * @a_intPageSize > @intRootRecordCount
return (-1)
/*求开始rootID*/
set @intRowCount = (@a_intPageNo - 1) * @a_intPageSize + 1
/*限制条数*/
set rowcount @intRowCount
select @intBeginID = rootid from bbs where fatherid=0 and forumid=@a_intForumID
order by id desc
/*结束rootID*/
set @intRowCount = @a_intPageNo * @a_intPageSize
/*限制条数*/
set rowcount @intRowCount
select @intEndID = rootid from bbs where fatherid=0 and forumid=@a_intForumID
order by id desc
/*恢复系统变量*/
set rowcount 0
set nocount off
select a.id , a.layer , a.forumid , a.subject , a.faceid , a.hits , a.time , a.UserID , a.fatherid , a.rootid ,
‘Bytes‘ = datalength(a.content) , b.UserName , b.Email , b.HomePage , b.Signature , b.Point
from bbs as a join BBSUser as b on a.UserID = b.ID
where Forumid=@a_intForumID and a.rootid between @intEndID and @intBeginID
order by a.rootid desc , a.ordernum desc
return(@@rowcount)
--select @@rowcount
go

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/66684/showart_1227039.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP