views:

44

answers:

2

i am adding data from vba excel using adodb into a mysql database

everything works great but is slow. the entire process takes about 5 seconds.

i believe the reason it is slow is because i am filtering it:

Dim rowid_batchinfo As String
rs.Filter = "datapath='" + dpath + "' and analystname='" + aname + "' and reportname='" + rname + "' and batchstate='" + bstate + "'"
If Not rs.EOF Then
    rowid_batchinfo = rs.Fields("rowid")
    cn.Execute "delete from batchinfo where rowid=" + rowid_batchinfo
    cn.Execute "delete from calibration where rowid='" + rowid_batchinfo + "'"
    cn.Execute "delete from qvalues where rowid='" + rowid_batchinfo + "'"
End If

i dont know exactly which process is to blame, but i am assumign the delete where is delaying me. one of the tables has about 500,000 rows, the other about 300,000 and the other 5,000.

here is the second part:

With rs
    .AddNew ' create a new record
    ' add values to each field in the record
    .Fields("datapath") = dpath
    .Fields("analysistime") = atime
    .Fields("reporttime") = rtime
    .Fields("lastcalib") = lcalib
    .Fields("analystname") = aname
    .Fields("reportname") = rname
    .Fields("batchstate") = bstate
    .Fields("instrument") = instrument
    .Update ' stores the new record
End With
' get the last id
Set rs = cn.Execute("SELECT @@identity", , adCmdText)
capture_id = rs.Fields(0)
'MsgBox capture_id
rs.Close
Set rs = Nothing

this part adds the data. i think that this is relatively quick, but i cannot be sure.

so for the delete statements, perhaps i should be creating an index? however, in this case, it will probably take time to create the index and i would need to drop it recreate it every time i need to do this.

anyone know how i can make this code work faster?

+1  A: 

an index on rowid for all tables would help

SQLMenace
how often? every time? how long does it take to create and drop an index?
I__
once and that is it, MySQL will then be able to use the index
SQLMenace
+1  A: 

I'm not seeing why you would need to drop the index. Just create it once on the rowid if it's not already defined as the primary key on each of those tables in which case it's already indexed.

Lastly instead of deleting and then reinserting data, you could perhaps if possible do an update which would be faster overall.

Khorkrak
thank you. i dont know much about indexes, how often do they update themselves?
I__
Every time you insert or delete a row the indexes on the affected tables will be adjusted. Also if you update a column that participates in an index and change its value on a row then the corresponding index will be updated as well.
Khorkrak