我应该为此创建索引吗?

时间:2010-06-02 21:43:08

标签: mysql sql excel vba

我正在使用adodb将数据从vba excel添加到mysql数据库

一切都很好但很慢。整个过程大约需要5秒钟。

我认为它缓慢的原因是因为我正在过滤它:

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

我不确切地知道哪个过程应该受到责备,但我认为delete where拖延了我。其中一个表有大约500,000行,另一个大约有300,000行,另外大约有5,000行。

这是第二部分:

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

这部分添加了数据。我认为这相对较快,但我无法确定。

所以对于delete语句,也许我应该创建一个索引?但是,在这种情况下,它可能需要一段时间来创建索引,我需要删除它,每次我需要这样做时重新创建它。

任何人都知道如何让这段代码更快地运作?

2 个答案:

答案 0 :(得分:1)

所有表的rowid索引都有帮助

答案 1 :(得分:1)

我不明白为什么你需要放弃索引。只需在rowid上创建一次,如果它尚未被定义为每个表上的主键,在这种情况下它已被索引。

最后,如果可能的话,您可以进行更新,而不是删除然后重新插入数据,而不是整体更新。