为什么打开与我的数据库的连接需要这么长时间?

时间:2019-06-20 03:39:42

标签: sql excel vba adodb

我创建了一些代码,以通过自定义函数将信息从sql数据库提取到excel中。目前,代码仅需5到7秒钟即可打开连接。代码的所有其他部分,包括查询本身,都非常快速。知道为什么要花这么长时间吗?

这是针对我在本地计算机上拥有的sqlsever的。

Function Lob_amt(sp_name As String, l_date As String) As Double


Dim Conn As ADODB.Connection
Set Conn = New ADODB.Connection
Dim recset As ADODB.Recordset
Set recset = New ADODB.Recordset

Dim l_year As Double
Dim l_name As String


l_year = Year(l_date)



Dim sqlQry As String, sConnect As String

sqlQry = "SELECT sum(lob_lobbying.Amount)FROM lob_lobbying" & _
    " WHERE lob_lobbying.Latest = 'Y'" & _
    " AND lob_lobbying.IndTot ='Y'" & _
    " AND UltOrg = '" & sp_name & "'" & _
    " AND CycleYear in ('" & l_year & "')"

sConnect = "Driver={SQL Server};Server=DESKTOP-L9CVIVP;Database=lobbying;Trusted_Connection=yes;"


Conn.Open sConnect

recset.Open sqlQry, Conn

If IsNull(recset.Fields(0).Value) Then
    Lob_amt = 0
    Else

    Lob_amt = recset.Fields(0).Value

    End If
recset.Close
Conn.Close
Set recset = Nothing
Set Conn = Nothing


End Function

我希望连接花费ms的时间,但是打开连接需要5到7秒的时间。用我当前的设置作为自定义函数,我将获得大约80,000个查询。我可以稍微更改设置,以便不再使用自定义功能,但是连接时间似乎异常长。

任何建议都值得赞赏!

1 个答案:

答案 0 :(得分:0)

尝试通过执行此配置文件来分析代码并研究即时窗口(由VBA编辑器中的Ctrl + G打开:

    Function Lob_amt(sp_name As String, l_date As String) As Double

    Debug.Print "begin Lob_amt function: " & Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
    Dim Conn As ADODB.Connection
    Set Conn = New ADODB.Connection
    Dim recset As ADODB.Recordset
    Set recset = New ADODB.Recordset

    Dim l_year As Double
    Dim l_name As String


    l_year = Year(l_date)



    Dim sqlQry As String, sConnect As String

    sqlQry = "SELECT sum(lob_lobbying.Amount)FROM lob_lobbying" & _
        " WHERE lob_lobbying.Latest = 'Y'" & _
        " AND lob_lobbying.IndTot ='Y'" & _
        " AND UltOrg = '" & sp_name & "'" & _
        " AND CycleYear in ('" & l_year & "')"
    Debug.Print vbNewLine & sqlQry & vbNewLine
    sConnect = "Driver={SQL Server};Server=DESKTOP-L9CVIVP;Database=lobbying;Trusted_Connection=yes;"


    Conn.Open sConnect
        Debug.Print "Lob_amt function Conn.Open: " & Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
    recset.Open sqlQry, Conn

    If IsNull(recset.Fields(0).Value) Then
        Lob_amt = 0
           Debug.Print "Lob_amt Lob_amt = 0: " & Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
        Else

        Lob_amt = recset.Fields(0).Value
                    Debug.Print "Lob_amt recset.Fields(0).Valu: " & Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
        End If

    recset.Close
    Debug.Print "Lob_amt recset closed: " & Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
    Conn.Close
    Debug.Print "Lob_amt conn closed: " & Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
    Set recset = Nothing
    Set Conn = Nothing


    End Function

问题可能在其他功能中作为模块

UPD: SELECT sum(lob_lobbying.Amount)FROM lob_lobbying WHERE lob_lobbying.Latest = 'Y' AND lob_lobbying.IndTot ='Y' AND UltOrg = 'Cigna Corp' AND CycleYear in ('2006') 此生成的查询将传递给Open方法,因此这些冻结(最多5秒钟)不是由于Open方法本身造成的,而是由于执行此查询而导致的,其中包含条件和组操作({{1 }})在SQL Server上,所以这是数据库问题。如果非常不舒服,则可以通过其他方法(索引,数据划分,OLAP等)解决该问题