一种理论上最快的Web数据库分页方法

首页
一种理论上最快的Web数据库分页方法
查看 编辑
Submitted by 声仔 on 日, 2004/07/18 - 4:42pm. ASP/PHP
作者:starleee

上篇我们谈到了关于数据库传统的三种分页方法和他们的利弊,并且提出了一种理论上最佳的分页方法,本篇我们就来详细说说这种最佳的分页方法。
首页
一种理论上最快的Web数据库分页方法
查看 编辑
Submitted by 声仔 on 日, 2004/07/18 - 4:42pm. ASP/PHP
作者:starleee

上篇我们谈到了关于数据库传统的三种分页方法和他们的利弊,并且提出了一种理论上最佳的分页方法,本篇我们就来详细说说这种最佳的分页方法。

一:构思。
在设计Web数据库时,如果我们要编历每一条纪录,那么只有采取分页模式才可以使Web数据库尽快,尽好的呈现给终端用户,也不会因为8秒原则而使用户失去浏览该页的兴趣。但是即使采取分页的办法,当出现多纪录的数据库时,也难免会使我们的用户感到翻页时速度太慢。就如同我的上篇文章说的那样,几乎上一片文章的三种分页方法都有一些缺陷。那么,我们如何做到能让数据库每次就取我们需要的纪录,这个很好实现,有游标返回多个纪录集就可以实现,但是如果让数据库的一端不会因为要刚好检索一页的纪录而大耗资源就很难了。最后,经过我的不断改写程序与测试,终于编写出了我认为理论上最快的Web数据库分页方法。

二:具体实现的存储过程。
我们结合一个BBS问题来谈谈这种方法。如何让一个BBS每次每页只现实需要的一页纪录呢?而我们需要提供给数据库有那些参数呢?可能会有以下参数。
第一:就是我们需要的当前的页数。
第二:当前定义的每一页的纪录集数目。这样你就可以根据需要在页面程序中修改每一页的纪录数。当然,如果你不考虑程序的可扩展性,你也可以在数据库里直接规定每一页有N条纪录即可。
第三:一个输出参数:就是从数据库里得出当前表中总纪录数目的多少。(注意,他不是一个页的纪录数目)他相当于ADO分页法中的Recordcount。如果你不需要总纪录数目可以不用返回他。
我们来看具体存储过程的代码。。。

CREATE PROCEDURE dbo.PRO_pageview
(

@tint_tableid tinyint=1, –这个是BBS的当前版面Id,你可以不用管他。。
@int_pagenow int=0,
@int_pagesize int=0,
@int_recordcount int=0 output –就是得出BBS某个版面的总贴数。。

)

AS
set nocount on

declare @int_allid int
declare @int_beginid int,@int_endid int
declare @int_pagebegin int, @int_pageend int

select @int_allid=count(*) from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid
select @int_recordcount=@int_allid –得出该版面的总贴数

declare cro_fastread cursor scroll
for select int_id from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid order by int_id desc –这里定义游标操作,但是不用临时纪录集,而且游标也不需要全部遍历所有纪录集。

open cro_fastread –打开游标
select @int_beginid=(@int_pagenow-1)*@int_pagesize+1 得出该页的第一个纪录Id
select @int_endid = @int_beginid+@int_pagesize-1 得出该页的最后一个纪录的Id

fetch absolute @int_beginid from cro_fastread into @int_pagebegin 将他的Id传给一个变量该页开始的Id
if @int_endid>@int_allid –这里要注意,如果某一页不足固定页数的纪录时。如只有一页纪录,而且纪录少于我们定义的数目。或者是最后一页时。。。
fetch last from cro_fastread into @int_pageend –直接将游标绝对定位到最后一条纪录,得出他的id号来。。。
else
fetch absolute @int_endid from cro_fastread into @int_pageend

select int_id,tint_level,tint_children,var_face,var_subject,datalength(txt_content) as int_len,sint_hits,var_url,var_image,var_user,dat_time,tint_tableid,bit_kernul from tab_discuss where tint_tableid=@tint_tableid and int_rootid between @int_pageend and @int_pagebegin order by int_rootid desc,num_order desc –我们就可以利用该页的第一个id和最后一个id得出中间的id来。。。。(注意。我们这个BBS的数性结构用了一种很巧妙的算法,就是用一个orderNum浮点数即可完成排序。。。)

–开始清场。。。
close cro_fastread
deallocate cro_fastread

return

我们再看看Asp页面里的程序操作。。。

pagenow=cint(request(”pagenow”)) –当前的页面。

if pagenow<=0 then pagenow=1
pagesize=10

set cmd=server.CreateObject(”adodb.command”)
cmd.ActiveConnection=strconn
cmd.CommandType=4
cmd.CommandText=”pro_pageview”

cmd.Parameters.Append cmd.CreateParameter(”tint_tableid”,adInteger,adParamInput,,tint_tableid)
cmd.Parameters.Append cmd.CreateParameter(”int_pagenow”,adInteger,adParamInput,,pagenow)
cmd.Parameters.Append cmd.CreateParameter(”int_pagesize”,adInteger,adParamInput,,pagesize)
cmd.Parameters.Append cmd.CreateParameter(”int_recordcount”,adInteger,adParamOutput)

set rs=cmd.Execute
if rs.eof then
Response.Write “目前已超过了纪录个数或纪录集为空!”
Response.End
end if

dim arrRs
arrRs=rs.getrows ‘可以利用getRows将纪录集迅速保存到一个二维的数组中来提高速度。

recordcount=cmd.Parameters(”int_recordcount”)
‘注意,当纪录不足以刚好整除单位页纪录时,我们也要将其定义为一页,如纪录数目为2页多一个纪录,此时我们的页数也要为3页纪录。
if (recordcount mod pagesize)=0 then
pagecount=recordcount\pagesize
else
pagecount=recordcount\pagesize+1
end if

<--分页开始 -->
固定的分页函数,其实无非是pagenow+1或pagenow-1,pagenow,pagecount

<%
‘———显示树性结构!————-
level=0
Response.Write “ “
for i=0 to ubound(arrRs,2)
if arrRs(1,i)>level then
Response.Write “ “
end if
if arrRs(1,i)for j=arrRs(1,i) to level-1
Response.Write “


next
end if
int_size=arrRs(5,i)
if int_size=0 then
str_size=” <无内容>“
else
str_size=”"
end if
Response.Write “

“&server.HTMLEncode(arrRs(4,i))&” “&str_size
if arrRs(7,i)<>“” then Response.Write ” <连接> “
if arrRs(8,i)<>“” then Response.Write ” <图像> “
Response.Write ” -【”&arrRs(9,i)&”】 “&arrRs(10,i)&” [ID:”&arrRs(0,i)&” 点击:”&arrRs(6,i)&”次] (”&int_size&”字节) (”&arrRs(2,i)&”)

level=arrRs(1,i)

next

Response.Write “


‘———显示树性结构完毕!————-

%>