欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一種理論上最快的Web數(shù)據(jù)庫分頁方法

 更新時間:2007年01月31日 00:00:00   作者:  
出了一種理論上最佳的分頁方法,本篇我們就來詳細說說這種最佳的分頁方法。 

一:構(gòu)思。 
  在設(shè)計Web數(shù)據(jù)庫時,如果我們要編歷每一條紀錄,那么只有采取分頁模式才可以使Web數(shù)據(jù)庫盡快,盡好的呈現(xiàn)給終端用戶,也不會因為8秒原則而使用戶失去瀏覽該頁的興趣。但是即使采取分頁的辦法,當(dāng)出現(xiàn)多紀錄的數(shù)據(jù)庫時,也難免會使我們的用戶感到翻頁時速度太慢。就如同我的上篇文章說的那樣,幾乎上一片文章的三種分頁方法都有一些缺陷。那么,我們?nèi)绾巫龅侥茏寯?shù)據(jù)庫每次就取我們需要的紀錄,這個很好實現(xiàn),有游標(biāo)返回多個紀錄集就可以實現(xiàn),但是如果讓數(shù)據(jù)庫的一端不會因為要剛好檢索一頁的紀錄而大耗資源就很難了。最后,經(jīng)過我的不斷改寫程序與測試,終于編寫出了我認為理論上最快的Web數(shù)據(jù)庫分頁方法。 

二:具體實現(xiàn)的存儲過程。 
  我們結(jié)合一個BBS問題來談?wù)勥@種方法。如何讓一個BBS每次每頁只現(xiàn)實需要的一頁紀錄呢?而我們需要提供給數(shù)據(jù)庫有那些參數(shù)呢?可能會有以下參數(shù)。 
  第一:就是我們需要的當(dāng)前的頁數(shù)。 
  第二:當(dāng)前定義的每一頁的紀錄集數(shù)目。這樣你就可以根據(jù)需要在頁面程序中修改每一頁的紀錄數(shù)。當(dāng)然,如果你不考慮程序的可擴展性,你也可以在數(shù)據(jù)庫里直接規(guī)定每一頁有N條紀錄即可。 
  第三:一個輸出參數(shù):就是從數(shù)據(jù)庫里得出當(dāng)前表中總紀錄數(shù)目的多少。(注意,他不是一個頁的紀錄數(shù)目)他相當(dāng)于ADO分頁法中的Recordcount。如果你不需要總紀錄數(shù)目可以不用返回他。 
  我們來看具體存儲過程的代碼。。。 

CREATE PROCEDURE dbo.PRO_pageview 


@tint_tableid tinyint=1,   --這個是BBS的當(dāng)前版面Id,你可以不用管他。。 
@int_pagenow int=0,       
@int_pagesize int=0, 
@int_recordcount int=0 output  --就是得出BBS某個版面的總貼數(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)操作,但是不用臨時紀錄集,而且游標(biāo)也不需要全部遍歷所有紀錄集。 
   
  open cro_fastread --打開游標(biāo) 
  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    --這里要注意,如果某一頁不足固定頁數(shù)的紀錄時。如只有一頁紀錄,而且紀錄少于我們定義的數(shù)目?;蛘呤亲詈笠豁摃r。。。 
    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   --我們就可以利用該頁的第一個id和最后一個id得出中間的id來。。。。(注意。我們這個BBS的數(shù)性結(jié)構(gòu)用了一種很巧妙的算法,就是用一個orderNum浮點數(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 "目前已超過了紀錄個數(shù)或紀錄集為空!" 
  Response.End 
end if 

dim arrRs 
arrRs=rs.getrows   '可以利用getRows將紀錄集迅速保存到一個二維的數(shù)組中來提高速度。 

recordcount=cmd.Parameters("int_recordcount") 
'注意,當(dāng)紀錄不足以剛好整除單位頁紀錄時,我們也要將其定義為一頁,如紀錄數(shù)目為2頁多一個紀錄,此時我們的頁數(shù)也要為3頁紀錄。 
if (recordcount mod pagesize)=0 then 
  pagecount=recordcount\pagesize 
else 
  pagecount=recordcount\pagesize+1 
end if 


<--分頁開始 --> 
<!-- #include file="include\tablepage.asp" -->固定的分頁函數(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)&" 點擊:"&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> 
<!-- 清場與除錯 --> 
<% 
rs.close 
set rs=nothing 
set cmd=nothing 
if err.number<>0 then Response.Redirect "bug.asp" 
%> 

三:特點 
  我們來看看他和傳統(tǒng)的三種方法的區(qū)別與特點 
  第一:每次只傳回一頁紀錄,而且只形成一個紀錄集,而且客戶端可以采用速度最快的火線游標(biāo)來完成頁面輸出。而不必像傳統(tǒng)的游標(biāo)法用rs.nextrecordset紀錄來輸出紀錄。 
  第二:數(shù)據(jù)庫沒有用到臨時表,這樣比轉(zhuǎn)儲紀錄集的速度大大提高。 
  第三:采用一個滾動游標(biāo),而且游標(biāo)只經(jīng)過二個操作就完成定位。速度也大大提高。 
   
  當(dāng)我采用了這種分頁方法時,已經(jīng)可以明顯的感覺出分頁速度的提高了。當(dāng)然,在處理樹型結(jié)構(gòu),數(shù)據(jù)庫運算時,我采用了許多方法盡可能的提高速度,如:采用二分區(qū)間法來編歷樹型結(jié)構(gòu),全部采用存儲過程來實現(xiàn)一切sql操作,采用觸發(fā)器和數(shù)據(jù)庫游標(biāo)來完成數(shù)據(jù)庫算法,這樣就盡量避免過多的網(wǎng)絡(luò)傳輸。任何操作只和數(shù)據(jù)庫進行一次參數(shù)傳遞就可以完成。 
  感興趣的朋友可以到我的論壇來看看:http://tec.xj169.net/bbs

相關(guān)文章

最新評論