should i be creating an index for this?
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 + "' a开发者_JAVA百科nd 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?
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.
an index on rowid for all tables would help
精彩评论