top
Loading...
一種理論上最快的Web數據庫分頁方法
上篇我們談到了關于數據庫傳統的三種分頁方法和他們的利弊,并且提出了一種理論上最佳的分頁方法,本篇我們就來詳細說說這種最佳的分頁方法。

一:構思。
在設計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=recordcountpagesize
else
pagecount=recordcountpagesize+1
end if


<--分頁開始 -->
<!-- #include file="includeablepage.asp" -->固定的分頁函數,其實無非是pagenow+1或pagenow-1,pagenow,pagecount
<!--分頁結束-->


<div align="left" class="pblank" >
<%
'---------顯示樹性結構!-------------
level=0
Response.Write "<ul>"
for i=0 to ubound(arrRs,2)
if arrRs(1,i)>level then
Response.Write "<ul>"
end if
if arrRs(1,i)<level then
for j=arrRs(1,i) to level-1
Response.Write "</ul>"
next
end if
int_size=arrRs(5,i)
if int_size=0 then
str_size=" <無內容>"
else
str_size=""
end if
Response.Write "<li><img src=face"&arrRs(3,i)&"><a href=showTitle.asp?int_id="&arrRs(0,i)&"&tint_tableid="&tint_tableid&" class=ptitle target=BoardAnnounce>"&server.HTMLEncode(arrRs(4,i))&"</a> "&str_size
if arrRs(7,i)<>"" then Response.Write " <連接> "
if arrRs(8,i)<>"" then Response.Write " <圖像> "
Response.Write " -【"&arrRs(9,i)&"】 <font color=444444>"&arrRs(10,i)&"</font>[<font color=920092>ID:"&arrRs(0,i)&"點擊:"&arrRs(6,i)&"次</font>]<font color=444444>("&int_size&"字節)</font><font color=ff0000>("&arrRs(2,i)&")</font></li>"


level=arrRs(1,i)

next

Response.Write "</ul>"
'---------顯示樹性結構完畢!-------------

%>
</div>
<div align="left">
<!--分頁開始 -->
<!-- #include file="includeablepage.asp" -->
<!--分頁結束-->
</div>
<!-- 清場與除錯 -->
<%
rs.close
set rs=nothing
set cmd=nothing
if err.number<>0 then Response.Redirect "bug.asp"
%>

三:特點
我們來看看他和傳統的三種方法的區別與特點
第一:每次只傳回一頁紀錄,而且只形成一個紀錄集,而且客戶端可以采用速度最快的火線游標來完成頁面輸出。而不必像傳統的游標法用rs.nextrecordset紀錄來輸出紀錄。
第二:數據庫沒有用到臨時表,這樣比轉儲紀錄集的速度大大提高。
第三:采用一個滾動游標,而且游標只經過二個操作就完成定位。速度也大大提高。

當我采用了這種分頁方法時,已經可以明顯的感覺出分頁速度的提高了。當然,在處理樹型結構,數據庫運算時,我采用了許多方法盡可能的提高速度,如:采用二分區間法來編歷樹型結構,全部采用存儲過程來實現一切sql操作,采用觸發器和數據庫游標來完成數據庫算法,這樣就盡量避免過多的網絡傳輸。任何操作只和數據庫進行一次參數傳遞就可以完成。
感興趣的朋友可以到我的論壇來看看:http://tec.xj169.net/bbs


(全文完:僅以此篇文章感謝蔣立新大哥對分頁問題的質疑,沒有他的執著探討,也不會有本文的誕生,還要感謝bigeagle與廖家遠,他們的分頁算法也給了我很多的啟發。)

北斗有巢氏 有巢氏北斗