[點(diǎn)晴永久免費(fèi)OA]【ASP】一種理論上最快的Web數(shù)據(jù)庫分頁方法
下面給出了一種理論上最佳的分頁方法,本篇我們就來詳細(xì)說說這種最佳的分頁方法。
一:構(gòu)思。 在設(shè)計(jì)Web數(shù)據(jù)庫時(shí),如果我們要編歷每一條記錄,那么只有采取分頁模式才可以使Web數(shù)據(jù)庫盡快,盡好的呈現(xiàn)給終端用戶,也不會(huì)因?yàn)?秒原則而使用戶失去瀏覽該頁的興趣。但是即使采取分頁的辦法,當(dāng)出現(xiàn)多記錄的數(shù)據(jù)庫時(shí),也難免會(huì)使我們的用戶感到翻頁時(shí)速度太慢。就如同我的上篇文章說的那樣,幾乎上一片文章的三種分頁方法都有一些缺陷。那么,我們?nèi)绾巫龅侥茏寯?shù)據(jù)庫每次就取我們需要的記錄,這個(gè)很好實(shí)現(xiàn),有游標(biāo)返回多個(gè)記錄集就可以實(shí)現(xiàn),但是如果讓數(shù)據(jù)庫的一端不會(huì)因?yàn)橐獎(jiǎng)偤脵z索一頁的記錄而大耗資源就很難了。最后,經(jīng)過我的不斷改寫程序與測試,終于編寫出了我認(rèn)為理論上最快的Web數(shù)據(jù)庫分頁方法。 二:具體實(shí)現(xiàn)的存儲(chǔ)過程。 我們結(jié)合一個(gè)BBS問題來談?wù)勥@種方法。如何讓一個(gè)BBS每次每頁只現(xiàn)實(shí)需要的一頁記錄呢?而我們需要提供給數(shù)據(jù)庫有那些參數(shù)呢?可能會(huì)有以下參數(shù)。 第一:就是我們需要的當(dāng)前的頁數(shù)。 第二:當(dāng)前定義的每一頁的記錄集數(shù)目。這樣你就可以根據(jù)需要在頁面程序中修改每一頁的記錄數(shù)。當(dāng)然,如果你不考慮程序的可擴(kuò)展性,你也可以在數(shù)據(jù)庫里直接規(guī)定每一頁有N條記錄即可。 第三:一個(gè)輸出參數(shù):就是從數(shù)據(jù)庫里得出當(dāng)前表中總記錄數(shù)目的多少。(注意,他不是一個(gè)頁的記錄數(shù)目)他相當(dāng)于ADO分頁法中的Recordcount。如果你不需要總記錄數(shù)目可以不用返回他。 我們來看具體存儲(chǔ)過程的代碼: create PROCEDURE dbo.PRO_pageview ( @tint_tableid tinyint=1, --這個(gè)是BBS的當(dāng)前版面Id,你可以不用管他 @int_pagenow int=0, @int_pagesize int=0, @int_recordcount int=0 output --就是得出BBS某個(gè)版面的總貼數(shù) ) 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 --得出該版面的總貼數(shù) 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 --這里定義游標(biāo)操作,但是不用臨時(shí)記錄集,而且游標(biāo)也不需要全部遍歷所有記錄集 open cro_fastread --打開游標(biāo) select @int_beginid=(@int_pagenow-1)*@int_pagesize+1 得出該頁的第一個(gè)記錄Id select @int_endid = @int_beginid+@int_pagesize-1 得出該頁的最后一個(gè)記錄的Id fetch absolute @int_beginid from cro_fastread into @int_pagebegin 將他的Id傳給一個(gè)變量該頁開始的Id if @int_endid>@int_allid --這里要注意,如果某一頁不足固定頁數(shù)的記錄時(shí);如只有一頁記錄,而且記錄少于我們定義的數(shù)目;或者是最后一頁時(shí)。 fetch last from cro_fastread into @int_pageend --直接將游標(biāo)絕對定位到最后一條記錄,得出他的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 --我們就可以利用該頁的第一個(gè)id和最后一個(gè)id得出中間的id來。。。。(注意。我們這個(gè)BBS的數(shù)性結(jié)構(gòu)用了一種很巧妙的算法,就是用一個(gè)orderNum浮點(diǎn)數(shù)即可完成排序。。。) --開始清場。。。 close cro_fastread deallocate cro_fastread return 我們再看看Asp頁面里的程序操作: pagenow=cint(request("pagenow")) --當(dāng)前的頁面。 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 "目前已超過了記錄個(gè)數(shù)或記錄集為空!" Response.End end if dim arrRs arrRs=rs.getrows '可以利用getRows將記錄集迅速保存到一個(gè)二維的數(shù)組中來提高速度。 recordcount=cmd.Parameters("int_recordcount") '注意,當(dāng)記錄不足以剛好整除單位頁記錄時(shí),我們也要將其定義為一頁,如記錄數(shù)目為2頁多一個(gè)記錄,此時(shí)我們的頁數(shù)也要為3頁記錄。 if (recordcount mod pagesize)=0 then pagecount=recordcount\pagesize else pagecount=recordcount\pagesize+1 end if <--分頁開始 --> <!-- #include file="include\tablepage.asp" -->固定的分頁函數(shù),其實(shí)無非是pagenow+1或pagenow-1,pagenow,pagecount <!--分頁結(jié)束--> <div align="left" class="pblank" > <% '---------顯示樹性結(jié)構(gòu)!------------- 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=" <無內(nèi)容>" 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)&" 點(diǎn)擊:"&arrRs(6,i)&"次</font>] <font color=444444>("&int_size&"字節(jié))</font> <font color=ff0000>("&arrRs(2,i)&")</font></li>" level=arrRs(1,i) next Response.Write "</ul>" '---------顯示樹性結(jié)構(gòu)完畢!------------- %> </div> <div align="left"> <!--分頁開始 --> <!-- #include file="include\tablepage.asp" --> <!--分頁結(jié)束--> </div> <!-- 清場與除錯(cuò) --> <% rs.close set rs=nothing set cmd=nothing if err.number<>0 then Response.Redirect "bug.asp" %> 三:特點(diǎn) 我們來看看他和傳統(tǒng)的三種方法的區(qū)別與特點(diǎn) 第一:每次只傳回一頁記錄,而且只形成一個(gè)記錄集,而且客戶端可以采用速度最快的火線游標(biāo)來完成頁面輸出。而不必像傳統(tǒng)的游標(biāo)法用rs.nextrecordset記錄來輸出記錄。 第二:數(shù)據(jù)庫沒有用到臨時(shí)表,這樣比轉(zhuǎn)儲(chǔ)記錄集的速度大大提高。 第三:采用一個(gè)滾動(dòng)游標(biāo),而且游標(biāo)只經(jīng)過二個(gè)操作就完成定位。速度也大大提高。 當(dāng)我采用了這種分頁方法時(shí),已經(jīng)可以明顯的感覺出分頁速度的提高了。當(dāng)然,在處理樹型結(jié)構(gòu),數(shù)據(jù)庫運(yùn)算時(shí),我采用了許多方法盡可能的提高速度,如:采用二分區(qū)間法來編歷樹型結(jié)構(gòu),全部采用存儲(chǔ)過程來實(shí)現(xiàn)一切sql操作,采用觸發(fā)器和數(shù)據(jù)庫游標(biāo)來完成數(shù)據(jù)庫算法,這樣就盡量避免過多的網(wǎng)絡(luò)傳輸。任何操作只和數(shù)據(jù)庫進(jìn)行一次參數(shù)傳遞就可以完成。 該文章在 2022/7/4 9:19:32 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |