如何加快 ADODB 连接

Posted

技术标签:

【中文标题】如何加快 ADODB 连接【英文标题】:How Can I Speed Up An ADODB Connection 【发布时间】:2019-03-28 20:39:48 【问题描述】:

我正在运行下面的代码,将 Access 数据库中的数据检索到 Excel 中。代码执行大约需要 1 分钟。目前大约有 8 列的 500 条记录。我可以做些什么来修改我的代码以更快地运行?

Sub sync_Data()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    Dim cn As ADODB.Connection, rs As ADODB.Recordset
    Dim mysqlSt As String
    Dim rowindex As Long

    mysqlSt = "SELECT pbsclients.client, pbsclients.priority, pbsclients.source, pbsclients.lastcontact, pbsclients.result, pbsclients.nextsteps, pbsclients.attempts, pbsclients.notes FROM pbsclients; "

    Set cn = New ADODB.Connection
    With cn
        .ConnectionString = con1
        .Open
    End With

    rowindex = 2
    Set rs = New ADODB.Recordset
    rs.Open mysqlSt, cn, adOpenDynamic, adLockOptimistic

    While Not rs.EOF
        Sheet3.Cells(rowindex, 1) = rs!client
        Sheet3.Cells(rowindex, 2) = rs!Priority
        Sheet3.Cells(rowindex, 3) = rs!Source
        Sheet3.Cells(rowindex, 4) = rs!lastcontact
        Sheet3.Cells(rowindex, 5) = rs!result
        Sheet3.Cells(rowindex, 6) = rs!nextsteps
        Sheet3.Cells(rowindex, 7) = rs!attempts
        Sheet3.Cells(rowindex, 8) = rs!Notes

        rowindex = rowindex + 1
        rs.MoveNext
    Wend

    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    Exit Sub

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
End Sub

【问题讨论】:

试试Range.CopyFromRecordset 而不是 While Not rs.EOFWend 之间的所有内容。 实际上,而不是整个 While...Wend 块。 FWIW While...Wend 是一个过时的构造,应该是 Do While...Loop 以允许 Exit Do 语句。 你需要以某种方式点击索引......尝试WHERE Id <> 0(假设是单列,自动编号PK) 试试看。假设员工是一个文本字段:WHERE ID <> 0 AND employee ='" & sheet3.range("D5") & "'" 【参考方案1】:

这是我的代码的工作版本,运行和检索大约需要 2 秒,而上面的代码需要 45 秒 - 1 分钟。

Sub sync_Data()
    Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Dim cn As ADODB.Connection, rs As ADODB.Recordset
Dim mysqlSt As String
Dim rowindex As Long


mysqlSt = "SELECT pbsclients.client, pbsclients.priority, pbsclients.source, pbsclients.lastcontact, pbsclients.result, pbsclients.nextsteps, pbsclients.attempts, pbsclients.notes FROM pbsclients WHERE Id <> 0 AND pbsclients.branch = '" & Sheet3.Range("Z1") & "'"

Set cn = New ADODB.Connection

With cn
    .ConnectionString = con1
    .Open
    End With
    rowindex = 2
    Set rs = New ADODB.Recordset
    rs.Open mysqlSt, cn, adOpenDynamic, adLockOptimistic

    Do While Not rs.EOF

    Sheet3.Range("A2").CopyFromRecordset rs

    Loop

    rs.Close
      cn.Close
    Set rs = Nothing

    Set cn = Nothing
    Exit Sub
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub

【讨论】:

你可以通过删除.CopyFromRecordset rs周围的Do While Not rs.EOF + Loop来使你的代码更清晰。

以上是关于如何加快 ADODB 连接的主要内容,如果未能解决你的问题,请参考以下文章

小表内连接大表,如何加快

如何使用 VBA 在 Excel 中添加连接(到外部数据源)并将其保存到该 Excel 电子表格的连接列表

如何加快 Spark 中的大数据框连接

如何在VBScript中使用ADODB结果集? [关闭]

如何加快 SQL 中的连接更新?我的陈述似乎无限期地运行

如何从 adodb 记录集中访问存储过程中的变量?