要能使记录以最快速度分页显示出来,请问哪位高手有好方法???
要能使记录以最快速度分页显示出来,请问哪位高手有好方法???
现有10W条数据,Access数据库保存
通过正常提取:
以下是引用片段:
<%
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("db2.mdb")
conn.Open connstr
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from people order by id desc"
rs.Open sql,conn,1,1
Do While Not rs.EOF
Response.write rs("id")&" | "
rs.MoveNext
Loop
%>
耗时3,250.000毫秒,总测试平均值在3秒左右
使用存储过程提取:
以下是引用片段:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db2.mdb")
cmd.ActiveConnection = conn
cmd.CommandText = "Select * from people order by id desc"
Set rs = cmd.Execute
Do While Not rs.EOF
Response.write rs("id")&" | "
rs.MoveNext
Loop
%>
耗时2,187.500毫秒,总测试平均值在2秒左右
以上两种均不能彻底解决执行时间漫长的问题,主要原因即是循环每次都须向数据库抽取记录(Command速度相对较快)
那么使用GetRows()方法呢:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db2.mdb")
cmd.ActiveConnection = conn
cmd.CommandText = "Select * from people order by id desc"
Set rs = cmd.Execute
rsArray = rs.GetRows()
For i = 0 To UBound(rsArray, 2)
Response.Write rsArray(0, i)&" | "
Next
%>
以下是引用片段:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db2.mdb")
cmd.ActiveConnection = conn
cmd.CommandText = "Select * from people order by id desc"
Set rs = cmd.Execute
Do While Not rs.EOF
Response.write rs("id")&" | "
rs.MoveNext
Loop
%>
耗时187.500毫秒,总测试平均值在0.2秒左右
GetRows()方法是将数据从 Recordset 复制到二维数组中,这是一个二维数组,第一个下标标识字段,第二个则标识记录号
所以rsArray = rs.GetRows()
rsArray(0, 0)就表示记录集第一行的第一字段值
rsArray(1, 0)就表示记录集第一行的第二字段值
数组的数据是保存在内存中的,这就从根本上解决了每次显示记录还需向数据库请求的麻烦。
版主推荐的是啥呢
分页的一个算法,主要是优化SQL查询语句,最底限度降低查询数据的记录量
<%
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("db2.mdb")
conn.Open connstr
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select TOP "& PageSize &" * From [table] where Id >= (select Max(Id) from (select top " & ((PageSize * (CurrentPage - 1)) + 1) & " Id from [table] order by Id) As tmp)) order by Id"
rs.Open sql,conn,1,1
Do While Not rs.EOF
Response.write rs("id")&" | "
rs.MoveNext
Loop
''参数说明:
''PageSize:每页需要显示的记录数
''CurrentPage:当前页码
%>