J
Jeff
I often read that ADO is supposed to be faster than DAO, but that's not the case in this particular
test. I wrote 3 procedures to test the speed of writing 100,000 records to a table using SQL, ADO,
and DAO, and DAO easily came out the fastest method.
First, I create a table called "table1" that has 5 fields of type BYTE. The weird nested loop in my
code to generate numbers is necessary for a procedure I will be using in another database. I also
used a delete query to empty the table after each test.
In the testsql procedure, I tested 3 different commands that execute SQL code; DoCmd.RunSQL took
325 seconds, CurrentProject.Connection.Execute took 215 seconds, and CurrentDb.Execute took 198
seconds. By the way, my computer is a 2ghz Pentium 4 with WindowsXP and Access 2002.
The testado procedure took 14 seconds.
The testdao procedure took just under 4 seconds.
Does anyone know how to do this with RDO? Is there another method that might be faster? I'll never
use SQL code in my VBA procedures again, that's for sure.
*** VBA code is below ***
Option Compare Database
Sub testsql()
Dim n0 As Byte, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte
Dim starttime As Single, finishtime As Single
SetOption "confirm action queries", False
starttime = Timer
For n0 = 0 To 9
For n1 = 0 To 9
For n2 = 0 To 9
For n3 = 0 To 9
For n4 = 0 To 9
sql = "insert into [table1] values (" & n0 & "," & n1 & "," & n2 & "," & n3 & "," & n4 &
")"
'DoCmd.RunSQL sql
'CurrentDb.Execute sql
CurrentProject.Connection.Execute sql
Next n4
Next n3
Next n2
Next n1
Next n0
finishtime = Timer
SetOption "confirm action queries", True
Debug.Print finishtime - starttime
End Sub
Sub testado()
Dim n0 As Byte, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte
Dim starttime As Single, finishtime As Single
Dim rs As New ADODB.Recordset
rs.Open "table1", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
starttime = Timer
For n0 = 0 To 9
For n1 = 0 To 9
For n2 = 0 To 9
For n3 = 0 To 9
For n4 = 0 To 9
rs.AddNew
rs(0) = n0
rs(1) = n1
rs(2) = n2
rs(3) = n3
rs(4) = n4
rs.Update
Next n4
Next n3
Next n2
Next n1
Next n0
rs.Close
Set rs = Nothing
finishtime = Timer
Debug.Print finishtime - starttime
End Sub
Sub testdao()
Dim n0 As Byte, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte
Dim starttime As Single, finishtime As Single
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("table1")
starttime = Timer
For n0 = 0 To 9
For n1 = 0 To 9
For n2 = 0 To 9
For n3 = 0 To 9
For n4 = 0 To 9
rs.AddNew
rs(0) = n0
rs(1) = n1
rs(2) = n2
rs(3) = n3
rs(4) = n4
rs.Update
Next n4
Next n3
Next n2
Next n1
Next n0
rs.Close
Set rs = Nothing
Set db = Nothing
finishtime = Timer
Debug.Print finishtime - starttime
End Sub
test. I wrote 3 procedures to test the speed of writing 100,000 records to a table using SQL, ADO,
and DAO, and DAO easily came out the fastest method.
First, I create a table called "table1" that has 5 fields of type BYTE. The weird nested loop in my
code to generate numbers is necessary for a procedure I will be using in another database. I also
used a delete query to empty the table after each test.
In the testsql procedure, I tested 3 different commands that execute SQL code; DoCmd.RunSQL took
325 seconds, CurrentProject.Connection.Execute took 215 seconds, and CurrentDb.Execute took 198
seconds. By the way, my computer is a 2ghz Pentium 4 with WindowsXP and Access 2002.
The testado procedure took 14 seconds.
The testdao procedure took just under 4 seconds.
Does anyone know how to do this with RDO? Is there another method that might be faster? I'll never
use SQL code in my VBA procedures again, that's for sure.
*** VBA code is below ***
Option Compare Database
Sub testsql()
Dim n0 As Byte, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte
Dim starttime As Single, finishtime As Single
SetOption "confirm action queries", False
starttime = Timer
For n0 = 0 To 9
For n1 = 0 To 9
For n2 = 0 To 9
For n3 = 0 To 9
For n4 = 0 To 9
sql = "insert into [table1] values (" & n0 & "," & n1 & "," & n2 & "," & n3 & "," & n4 &
")"
'DoCmd.RunSQL sql
'CurrentDb.Execute sql
CurrentProject.Connection.Execute sql
Next n4
Next n3
Next n2
Next n1
Next n0
finishtime = Timer
SetOption "confirm action queries", True
Debug.Print finishtime - starttime
End Sub
Sub testado()
Dim n0 As Byte, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte
Dim starttime As Single, finishtime As Single
Dim rs As New ADODB.Recordset
rs.Open "table1", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
starttime = Timer
For n0 = 0 To 9
For n1 = 0 To 9
For n2 = 0 To 9
For n3 = 0 To 9
For n4 = 0 To 9
rs.AddNew
rs(0) = n0
rs(1) = n1
rs(2) = n2
rs(3) = n3
rs(4) = n4
rs.Update
Next n4
Next n3
Next n2
Next n1
Next n0
rs.Close
Set rs = Nothing
finishtime = Timer
Debug.Print finishtime - starttime
End Sub
Sub testdao()
Dim n0 As Byte, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte
Dim starttime As Single, finishtime As Single
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("table1")
starttime = Timer
For n0 = 0 To 9
For n1 = 0 To 9
For n2 = 0 To 9
For n3 = 0 To 9
For n4 = 0 To 9
rs.AddNew
rs(0) = n0
rs(1) = n1
rs(2) = n2
rs(3) = n3
rs(4) = n4
rs.Update
Next n4
Next n3
Next n2
Next n1
Next n0
rs.Close
Set rs = Nothing
Set db = Nothing
finishtime = Timer
Debug.Print finishtime - starttime
End Sub