抽取10万条数据,想起GetRows()

时间:2022-06-01 17:59:45

现有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
%>


http://www.cnbruce.com/test/getrows/show1.asp 

耗时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
%>


http://www.cnbruce.com/test/getrows/show2.asp 

耗时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
%>


http://www.cnbruce.com/test/getrows/show3.asp 

耗时187.500毫秒,总测试平均值在0.2秒左右

GetRows()方法是将数据从 Recordset 复制到二维数组中,这是一个二维数组,第一个下标标识字段,第二个则标识记录号

所以rsArray = rs.GetRows()
rsArray(0, 0)就表示记录集第一行的第一字段值
rsArray(1, 0)就表示记录集第一行的第二字段值

数组的数据是保存在内存中的,这就从根本上解决了每次显示记录还需向数据库请求的麻烦。