高效分頁 asp

來源:互聯網
上載者:User

標籤:des   style   color   java   使用   os   資料   ar   

<%
‘每頁的記錄數
dim pagesize
pagesize= "30"

‘讀出總記錄數,總頁數,作者注
Dim TotalRecords,TotalPages
SQLstr="Select count(id) As RecordSum From table1"
Set Rs=conn.Execute(SQLstr,0,1)
TotalRecords=Rs("RecordSum")
if Int(TotalRecords/pagesize)=TotalRecords/pagesize then
TotalPages=TotalRecords/pagesize
else
TotalPages=Int(TotalRecords/pagesize)+1
end if
Rs.Close
Set Rs=Nothing

‘當前頁碼,作者注
dim page
page=Request("page")
if isnumeric(page)=false then
response.write "<SCRIPT language=JavaScript>alert(‘參數錯誤!‘);"
response.write "window.close();</SCRIPT>"
response.end
end if
If page="" or page<1 Then page=1
If page-TotalPages>0 Then page=TotalPages
page=int(page)

if page=1 then
sql="select top "&pagesize&" id,title,time from table1 order by time desc"
else
sql="select top "&pagesize&" id,title,time from table1 where time<(SELECT Min(time) FROM (SELECT TOP "&pagesize*(page-1)&" time FROM table1 ORDER BY time desc) AS T) order by time desc"
end if
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,1,1
Do While Not rs.Eof
response.write "每條記錄資訊:"&rs("id")&"<br>"
rs.movenext
loop
rs.close
set rs=nothing
‘‘翻頁代碼省略……
%>

    這是一種非常高效的分頁演算法。當資料表中的資料量成百上千萬的時候,上面的這種分頁演算法的回應時間是非常短的,通常在幾十毫秒之內。原理很簡單,就是每次分頁,我只取需要的幾十條記錄而已,使用SELECT TOP也正是基於這樣的考慮。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.