I'm doing some speed tests. I created a brand-new table, with just
one "Long Integer" field. I'm testing adding 1000 records. If I use
the "Export" feature in Access, it takes only a few seconds to export
the 1000 records to a new table on the server. If I use my code
below, it takes 100 secods (10 records per second). Can someone help
me speed up my code?
--- code ---
Dim con As New ADODB.Connectio n
con.Open "Provider=MSDAS QL.1;DRIVER={My SQL ODBC 3.51
Driver};SERVER= www.mydomain.co m;PORT=3306;OPT ION=3;DATABASE= mydbname;UID=my uid;PWD=mypwd;"
'con.BeginTrans
Dim rs As New ADODB.Recordset
rs.Open "Table1", con, adOpenKeyset, adLockOptimisti c
' This loop takes ~100 seconds
For x = 1 To 1000
rs.AddNew
rs.Fields("ID") .Value = x
rs.Update
Next x
rs.Close
'con.CommitTran s
con.Close
---- end code ----
Please note I had to comment out the BeginTrans and CommitTrans
because I was getting an error at CommitTrans saying that "No
Transaction has been started". I think this is because either the
ODBC driver (MyODBC) or the server (MySQL, with MyISAM) doesn't
support transactions (probably because I'm using MyISAM data format,
with a 3.x version of MySQL server).
At any rate, export is fast, so there must be a way to speed up adding
records. I can't just use export because I actually need more control
than just dumping records over, but for now I'm just trying to find
the fastest method for adding several records.
I think the above code is sending an "INSERT INTO" SQL statement to
the server for each record I add. Is there another method I can use,
without hand-coding the SQL myself, that would send all 1000 adds as a
single INSERT INTO statement? MySQL does support multiple rows in the
VALUES() part of the INSERT INTO statement. If possible I'd like to
stick with code that also works with MSSQL, or would work with only
minor changes... which is one reason I haven't tried any native MySQL
APIs.
The reason I posted this to the MSAccess group instead of MySQL/MyODBC
is to see if anyone knows what method MSAccess uses for Export, why is
it so much faster? And also because my code is in VBA. I'll probably
setup netmon.. maybe I can see the actual SQL statements being sent by
each method.
one "Long Integer" field. I'm testing adding 1000 records. If I use
the "Export" feature in Access, it takes only a few seconds to export
the 1000 records to a new table on the server. If I use my code
below, it takes 100 secods (10 records per second). Can someone help
me speed up my code?
--- code ---
Dim con As New ADODB.Connectio n
con.Open "Provider=MSDAS QL.1;DRIVER={My SQL ODBC 3.51
Driver};SERVER= www.mydomain.co m;PORT=3306;OPT ION=3;DATABASE= mydbname;UID=my uid;PWD=mypwd;"
'con.BeginTrans
Dim rs As New ADODB.Recordset
rs.Open "Table1", con, adOpenKeyset, adLockOptimisti c
' This loop takes ~100 seconds
For x = 1 To 1000
rs.AddNew
rs.Fields("ID") .Value = x
rs.Update
Next x
rs.Close
'con.CommitTran s
con.Close
---- end code ----
Please note I had to comment out the BeginTrans and CommitTrans
because I was getting an error at CommitTrans saying that "No
Transaction has been started". I think this is because either the
ODBC driver (MyODBC) or the server (MySQL, with MyISAM) doesn't
support transactions (probably because I'm using MyISAM data format,
with a 3.x version of MySQL server).
At any rate, export is fast, so there must be a way to speed up adding
records. I can't just use export because I actually need more control
than just dumping records over, but for now I'm just trying to find
the fastest method for adding several records.
I think the above code is sending an "INSERT INTO" SQL statement to
the server for each record I add. Is there another method I can use,
without hand-coding the SQL myself, that would send all 1000 adds as a
single INSERT INTO statement? MySQL does support multiple rows in the
VALUES() part of the INSERT INTO statement. If possible I'd like to
stick with code that also works with MSSQL, or would work with only
minor changes... which is one reason I haven't tried any native MySQL
APIs.
The reason I posted this to the MSAccess group instead of MySQL/MyODBC
is to see if anyone knows what method MSAccess uses for Export, why is
it so much faster? And also because my code is in VBA. I'll probably
setup netmon.. maybe I can see the actual SQL statements being sent by
each method.
Comment