Excel 刷新每个查询所用的 ODBC 数据连接查询时间

Excel ODBC Data Connection Query time taken to refresh each query

我正在尝试测试来自 Excel 数据连接的 运行 查询的三种变体。

我有三个单独的数据连接和三个单独的选项卡,分别从每个连接获取数据。

每个查询的连接字符串都相同,只有命令文本 (Oracle SQL) 不同。

Is there a way in Excel to view the execution times for each query?

我具体用的是版本Excel 2016 MSO 16.0.4456.1003 64bit

可能是这样的(假设所有连接都将其结果放在工作表 table 中,而不是数据透视表 table 中):

Sub TimeQueries()
    Dim oSh As Worksheet
    Dim oCn As WorkbookConnection
    Dim dTime As Double
    For Each oCn In ThisWorkbook.Connections
        dTime = Timer
        oCn.Ranges(1).ListObject.QueryTable.Refresh False
        Debug.Print Timer - dTime, oCn.Name, oCn.Ranges(1).Address(external:=True)
    Next
End Sub

给运行这个:

  1. Alt+F11 转到 VBA 编辑器。
  2. 从菜单:插入模块。
  3. 在 window 中粘贴代码。
  4. 关闭VBA 编辑器。
  5. Alt+F8 调出宏列表。选择新的并单击 运行。
  6. Alt+F11 再次进入 VBA 编辑器。
  7. Ctrl+G 打开包含结果的即时窗格。

如果您希望代码写入单元格,请使用此版本:

Sub TimeQueries()
    Dim oSh As Worksheet
    Dim oCn As WorkbookConnection
    Dim dTime As Double
    Dim lRow As Long
    Set oSh = Worksheets("Sheet4") 'Change to your sheet name!
    oSh.Cells(1,1).Value = "Name of Connection"
    oSh.Cells(1,2).Value = "Location"
    oSh.Cells(1,1).Value = "Refresh time (s)"
    For Each oCn In ThisWorkbook.Connections
        lRow = lRow + 1
        dTime = Timer
        oCn.Ranges(1).ListObject.QueryTable.Refresh False
        oSh.Cells(lRow,3).Value = Timer - dTime
        oSh.Cells(lRow,1).Value = oCn.Name
        oSh.Cells(lRow,2).Value = oCn.Ranges(1).Address(external:=True)
    Next
End Sub