I was doing a mini speed test on looping recordsets - a task that I seem to do all the time in my programs...
Results:
Static,Optimistic, Explicit fields : 6.9180
ForwardOnly,ReadOnly (default), Explicit fields : 0.5117
ForwardOnly,ReadOnly (default), Predefined fields : 0.3125
I was kind of suprised by the last one, Using predefined fields [set f= Rst.fields(..)] is >60 % faster than the explicit method (Rst.fields(..)].
Commments, improvements?
Test code:
---------------------------------------------------------
[sup]
Option Explicit
Private Sub Command1_Click()
Const n As String = "10000"
Dim conn As Connection
Dim Rst As Recordset
Dim s As String, t As Single
Dim f1 As ADODB.Field
Dim f2 As ADODB.Field
Set conn = New Connection
conn.Open "Provider=SQL OLEDB;Integrated Security=SSPI;Persist Security Info=False;User ID=dbo;Initial Catalog=;Data Source="
Set Rst = New Recordset
Rst.Open "SELECT top " & n & " D.Data, D.Pres FROM tblData D", conn, adOpenStatic, adLockOptimistic
t = Timer
If Not Rst.EOF And Not Rst.BOF Then
Do
s = CStr(Rst.Fields("Data") & " " & CStr(Rst.Fields("Data")
Rst.MoveNext
Loop Until Rst.EOF
End If
Rst.Close
Set Rst = Nothing
Text1.Text = "Static,Optimistic, Explicit fields : " & Format(Timer - t, "0.0000" & vbCrLf
Set Rst = New Recordset
Rst.Open "SELECT top " & n & " D.Data, D.Pres FROM tblData D", conn
t = Timer
If Not Rst.EOF And Not Rst.BOF Then
Do
s = CStr(Rst.Fields("Data") & " " & CStr(Rst.Fields("Data")
Rst.MoveNext
Loop Until Rst.EOF
End If
Rst.Close
Set Rst = Nothing
Text1.Text = Text1.Text & "ForwardOnly,ReadOnly (default), Explicit fields : " & Format(Timer - t, "0.0000" & vbCrLf
Set Rst = New Recordset
Rst.Open "SELECT top " & n & " D.Data, D.Pres FROM tblData D", conn
t = Timer
Set f1 = Rst.Fields("Data"
Set f2 = Rst.Fields("pres"
If Not Rst.EOF And Not Rst.BOF Then
Do
s = CStr(f1) & " " & CStr(f2)
Rst.MoveNext
Loop Until Rst.EOF
End If
Rst.Close
Set Rst = Nothing
Text1.Text = Text1.Text & "ForwardOnly,ReadOnly (default), Predefined fields : " & Format(Timer - t, "0.0000" & vbCrLf
conn.Close
Set conn = Nothing
End Sub
[/sup]
---------------------------------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
Results:
Static,Optimistic, Explicit fields : 6.9180
ForwardOnly,ReadOnly (default), Explicit fields : 0.5117
ForwardOnly,ReadOnly (default), Predefined fields : 0.3125
I was kind of suprised by the last one, Using predefined fields [set f= Rst.fields(..)] is >60 % faster than the explicit method (Rst.fields(..)].
Commments, improvements?
Test code:
---------------------------------------------------------
[sup]
Option Explicit
Private Sub Command1_Click()
Const n As String = "10000"
Dim conn As Connection
Dim Rst As Recordset
Dim s As String, t As Single
Dim f1 As ADODB.Field
Dim f2 As ADODB.Field
Set conn = New Connection
conn.Open "Provider=SQL OLEDB;Integrated Security=SSPI;Persist Security Info=False;User ID=dbo;Initial Catalog=;Data Source="
Set Rst = New Recordset
Rst.Open "SELECT top " & n & " D.Data, D.Pres FROM tblData D", conn, adOpenStatic, adLockOptimistic
t = Timer
If Not Rst.EOF And Not Rst.BOF Then
Do
s = CStr(Rst.Fields("Data") & " " & CStr(Rst.Fields("Data")
Rst.MoveNext
Loop Until Rst.EOF
End If
Rst.Close
Set Rst = Nothing
Text1.Text = "Static,Optimistic, Explicit fields : " & Format(Timer - t, "0.0000" & vbCrLf
Set Rst = New Recordset
Rst.Open "SELECT top " & n & " D.Data, D.Pres FROM tblData D", conn
t = Timer
If Not Rst.EOF And Not Rst.BOF Then
Do
s = CStr(Rst.Fields("Data") & " " & CStr(Rst.Fields("Data")
Rst.MoveNext
Loop Until Rst.EOF
End If
Rst.Close
Set Rst = Nothing
Text1.Text = Text1.Text & "ForwardOnly,ReadOnly (default), Explicit fields : " & Format(Timer - t, "0.0000" & vbCrLf
Set Rst = New Recordset
Rst.Open "SELECT top " & n & " D.Data, D.Pres FROM tblData D", conn
t = Timer
Set f1 = Rst.Fields("Data"
Set f2 = Rst.Fields("pres"
If Not Rst.EOF And Not Rst.BOF Then
Do
s = CStr(f1) & " " & CStr(f2)
Rst.MoveNext
Loop Until Rst.EOF
End If
Rst.Close
Set Rst = Nothing
Text1.Text = Text1.Text & "ForwardOnly,ReadOnly (default), Predefined fields : " & Format(Timer - t, "0.0000" & vbCrLf
conn.Close
Set conn = Nothing
End Sub
[/sup]
---------------------------------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'