欢迎光临
我们一直在努力

关于存储过程分页

建站超值云服务器,限时71元/月

看了几个朋友写的关于存储过程分页的文章,感觉有点问题。starleee和东方蜘蛛希望我能发表点看法,我简单说一下。

首先是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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 关于存储过程分页
分享到: 更多 (0)