近日一直在研究如何才能写出高小的分页算法,大概整理了一下,思路如下:
首先数据库里需要有一个自动编号字段(ID)。然后第一次访问的时候,取出所有记录,定制好每页的记录数PageSize,计算出页数,然后根据页数建立一个一维数组PageId(PageCount),PageId(0)保存记录初试条件,然后对应每个元素保存每页对应的ID边界码
(
1,ID边界码:如果数据库记录ID记录序列如下 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
假设需要按照ID 顺序排序的话 ,PageSize = 5, Pagecount = 4 ,PageId(4)
数组PageId的值分别为PageId(0) = 1, PageId(1) = 5 ,PageId(2) = 10,PageId(3) = 15 ,PageId(4) = 16
当访问第 i 页的时候就直接找 [PageId(i-1) , PageId(i) ) 之间的记录,这样可以保证每次取的记录都只是PageSize 条记录。
假设需要按照ID倒序排列的话,
数组PageId的值分别为PageId(0) = 16 , PageId(1) = 12 , PageId(2) = 7 ,PageId(3) = 2, PageId(4) = 1, 当访问第 i 页的时候就直接查找ID属于[ PageId(i-1) , PageId(i) )
)
将数组PageId()保存在Application()中,以便访问,这样,只是第一次访问分页程序的时候便初始化Application()。代码部分如下:(下面称为新程序)
<%
Time1 = Timer()
Dim Conn
Set Conn = Server.CreateObject("Adodb.Connection")
Conn.open "Driver={MicroSoft Access Driver (*.mdb)};Dbq="&Server.MapPath("db.mdb")
Dim Page,PageCounts,PageId,PageList
Dim Rs,Sql
Dim IsInit,i
IsInit = False '标志为,用来判断Application("PageId")是否初始化
PageList = 20 '设置每页显示20条数据
Set Rs = Server.CreateObject("Adodb.Recordset")
Page = Request.QueryString("Page") '注意页码需要检查类型
If IsEmpty(Application("PageId")) Then '如果Application("PageId")还未初始化,则先进行初始化
Response.Write("Init app!<br>")
Sql = "Select * From test Order By Id Desc" '假定这里是按照ID倒序排列
Rs.open Sql,Conn,1,1 '得到记录集对象
If Not (Rs.Eof or Rs.Bof) Then
Rs.PageSize = PageList '设置每页记录数
PageCounts = Rs.PageCount
ReDim PageId(PageCounts) '重新定义数组PageId
For i = 0 To PageCounts '开始给数组 PageId() 赋值
If Rs.eof Then Exit For
PageId(i) = Rs("ID")
Rs.Move (PageList)
Next
Rs.MoveLast
PageId(PageCounts) = Rs("ID")
Application.Lock()
Application("PageId") = PageId
Application.UnLock()
End If
Rs.Close
End If
IdStart = Clng(Application("PageId")(Page-1))
IdEnd = Clng(Application("PageId")(Page))
Sql = "Select * from test where id<="&IdStart&" and id>"&IdEnd&" "
Rs.open Sql,Conn,1,1
While Not Rs.eof
Response.Write(rs(0)&"--"&rs(1))
Rs.MoveNext
Wend
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
For i = 1 To Ubound(Application("PageId"))
Response.Write("<a href='Test1.asp?Page="&i&"'>"&i&"</a> ")
Next
Time2 = Timer()
Response.Write("<br>"&(Time2-Time1)*1000)
'Application.Contents.Remove("PageId")
%>
传统分页代码如下:(下面称为旧程序)
<%
Time1 = Timer()
Dim Conn
Set Conn = Server.CreateObject("Adodb.Connection")
Conn.open "Driver={MicroSoft Access Driver (*.mdb)};Dbq="&Server.MapPath("db.mdb")
Dim Page,PageCounts,PageList
Dim Rs,Sql
PageList = 20
Page = Request.QueryString( "Page" )
Set Rs = Server.CreateObject("Adodb.Recordset")
Sql = "Select * from test order by id desc"
Rs.Open Sql,Conn,1,1
If Page = "" Then Page = 1
If Not( Rs.eof Or Rs.Bof ) Then
Rs.PageSize = PageList
PageCounts = Rs.PageCount
Rs.AbsolutePage = Page
End If
For i = 1 to PageList
If Rs.eof Then Exit For
Response.Write(Rs(0)&"-----"&Rs(1)&"<br>")
Rs