MS Access数据库增长问题

时间:2016-07-08 15:37:51

标签: database access-vba ms-access-2010

我创建了下面一段代码,以便修改Access表中的一些数据:

Dim Ways As DAO.Recordset
Dim Keys As DAO.Recordset
Dim Recordcount As Double
Dim Records As Double
Dim ID_Old As String
Dim ID_New As String
Dim STArray() As String
Dim SaveTime As String

Set Ways = CurrentDb.OpenRecordset("Ways_Sorted")

Recordcount = 1
Records = 3724755


Ways.MoveFirst

Dim word As Variant

    While Not Ways.EOF
    DoCmd.SetWarnings (False)
        DoCmd.OpenQuery "KeyFind:DEL"
    DoCmd.SetWarnings (True)

     Set Keys = CurrentDb.OpenRecordset("KeyFind")

        STArray = Split(Ways!Veld4, ";")
        For Each word In STArray
            If Len(word) > 0 Then
                Keys.AddNew
                Keys!IDOld = CDbl(word)
                Keys!IDNew = DLookup("[New ID]", "ID Keys", "[Old ID]=" & CDbl(word))
                Keys.Update
            End If
        Next

        Keys.MoveFirst
        While Not Keys.EOF
        ID_Old = " " + Trim(Str$(Keys!IDOld))
        ID_New = " " + Trim(Str$(Keys!IDNew))
            Ways.Edit
            Ways!Veld4 = Replace(Ways!Veld4, ID_Old, ID_New)
            Keys.MoveNext
        Wend
        Keys.Close

        Me.Tekst1 = Recordcount
        Me.Tekst3 = Records - Recordcount
        Me.Tekst5 = FileLen(Application.CurrentProject.Path & "\Map_Convert_2.mdb")
        If FileLen(Application.CurrentProject.Path & "\Map_Convert_2.mdb") > 1977142784 Then
                '            Exit Sub
        End If

        DoEvents
        Ways!Done = True
        Ways.Update
        Ways.MoveNext
        Recordcount = Recordcount + 1
        'CommandBars("Menu Bar").Controls("Tools").Controls("Database utilities").Controls("Compact and repair database...").accDoDefaultAction
        'Stop
    Wend

DoCmd.SetWarnings (False)
    DoCmd.OpenQuery "Ways_Amend ID"
DoCmd.SetWarnings (True)

MsgBox "New Map created"

实际上代码正在做的是替换表“Ways_Sorted”中字段“Veld4”中的数据。该字段包含一个ID为ID的字符串,该字符串将STArray = Split(Ways!Veld4, ";")拆分为一个数组。 该数组存储在名为“KeysFound”的表中。 我的数据库中的另一个表包含旧ID和新ID。 如上所述,其余代码将使用新ID替换“Veld4”中的旧ID。 它通过这种方式循环播放370万条记录。

我的问题是,在250次循环后,我的数据库增长了1mB,这意味着我的数据库将在代码完成之前超过2gB。

我无法解释为什么增长正在发生以及如何阻止这种增长或减少增长

1 个答案:

答案 0 :(得分:0)

您的代码有很多优化的潜力。

主要问题:您经常在Keys表中写入和删除。我想这也是增长问题的原因。

这个表是不必要的。只需在阅读每个密钥后立即进行更换。将新的Veld4构建为字符串NewVeld,只有在完成当前Ways行后才将其写入表中。

    STArray = Split(Ways!Veld4, ";")
    NewVeld = ""
    For Each word In STArray
        If Len(word) > 0 Then
            NewKey = DLookup("[New ID]", "ID Keys", "[Old ID]=" & CDbl(word))
            ' you will have to adapt this to your exact "veld" structure
            ' If there is a specific reason, you can also continue to use Replace(),
            ' but I don't think it's necessary.
            NewVeld = NewVeld & ";" & NewKey
        End If
    Next
    ' remove leading ";"
    NewVeld = Mid(NewVeld, 2)

    Ways.Edit
    Ways!Veld4 = NewVeld
    Ways!Done = True
    Ways.Update
    Ways.MoveNext

进一步优化:DLookup对于您的行计数来说是一项相当昂贵的操作 考虑将整个ID Keys表格加载到开头的Dictionary object,然后从那里读取新的ID。