ADODB Command.Execute 超时
ADODB Command.Execute Timeout
当 运行 来自 Excel vba
的查询时,我看到超时错误
从 SQL Server Management Studio 查询需要 <2 秒,但从 vba 超时发生在 2 分钟,没有任何返回
我在设置 Command 对象时有没有做错?我注意到 ADODB 似乎更慢,但从来没有像这样
查询连接了几个表并进行了一些其他计算,但是从 1.5 秒到 >2 分钟一定意味着 vba 中的某些内容我错过了
这是我的 vba 连接字符串代码:
If svrCon Is Nothing Then
Set svrCon = New ADODB.Connection
End If
If Not CheckServerConnectionState Then
conStr = "Provider=SQLOLEDB;Data Source=ussantapps332;" & _
"Initial Catalog=Global_OEE_Data_Capture_Dev;User Id=sqluser;Password=*****;"
' Open the connection
svrCon.ConnectionTimeout = 0
svrCon.Open conStr
End If
这是我的vbaSELECT代码:
Dim cmd As ADODB.Command
Dim par As ADODB.Parameter
Dim rst As ADODB.Recordset
' Create command object
Set cmd = New ADODB.Command
cmd.CommandTimeout = 120
cmd.ActiveConnection = svrCon
cmd.CommandText = sql
' Create parameter object
If IsArrayInitialized(params) Then
For x = 0 To UBound(params)
If IsNull(params(x, 1)) Then
Set par = cmd.CreateParameter(Type:=params(x, 0), Size:=1)
Else
Set par = cmd.CreateParameter(Type:=params(x, 0), Size:=Len(params(x, 1)) + 1)
End If
par.Value = params(x, 1)
cmd.Parameters.Append par
Set par = Nothing
DoEvents
Next
End If
' Open recordset object
On Error GoTo ExecuteError
Debug.Print Format(Now, "hh:mm:ss")
Set rst = cmd.Execute
Debug.Print Format(Now, "hh:mm:ss")
On Error GoTo 0
将sql字符串和参数传入函数,连接从另一个方法打开
查询是:
SELECT U.UnitsID, L.LineName, V.VSName, O.OperatorShift, O.LineLeader, O.CotyOps, O.TempOps, U.WorkOrder, U.ProductCode,
S.ProdDesc, U.TimeLineStart, U.TimeLineEnd, U.UnitsProduced, U.ActLineSpeed, U.TgtLineSpeed, SUM(CASE WHEN C.DTIncludedInOEE = 0 THEN D.DowntimeLength ELSE 0 END),
U.OfflineTaskID, R.Rate, S.LabHrsPerThou, S.PHeads, T.TgtOEE, T.TgtEff, T.TgtProd
FROM dataUnits U
LEFT JOIN dataOperatorNames O ON O.OperatorID = U.OperatorNameID
INNER JOIN setupLines L ON U.LineID = L.LineID
INNER JOIN setupValueStreams V on V.VSID = L.VSID
INNER JOIN setupPUs P ON V.PUID = P.PUID
LEFT JOIN dataDowntimes D ON U.UnitsID = D.UnitsID
LEFT JOIN setupDowntimes sD ON D.DTID = sD.DTID
LEFT JOIN setupDowntimeCats C ON sD.DTCatID = C.DTCatID
LEFT JOIN (SELECT VSID, AVG(RateVal) Rate
FROM dataRates WHERE FYStart >= '2014-07-01' AND FYStart < '2015-07-01'
GROUP BY VSID) R ON R.VSID = L.VSID
LEFT JOIN dataStandards S ON S.ProdCode = U.ProductCode
LEFT JOIN (SELECT LineID, AVG(TgtOEE) TgtOEE, AVG(TgtEff) TgtEff, AVG(TgtProd) TgtProd
FROM dataTargets WHERE TgtMonth >= '2015-03-01' AND TgtMonth < '2015-04-01'
GROUP BY LineID) T ON L.LineID = T.LineID
WHERE (S.SAPVersion = (SELECT MIN(SAPVersion) FROM dataStandards s2 WHERE s2.ProdCode = S.ProdCode)
OR S.SAPVersion IS NULL)
AND P.SiteID = 2 AND U.TimeLineStart >= '2015-03-05 23:00' AND U.TimeLineStart < '2015-03-31 23:00'
GROUP BY U.UnitsID, L.LineName, V.VSName, O.OperatorShift, O.LineLeader, O.CotyOps, O.TempOps, U.WorkOrder, U.ProductCode, S.ProdDesc, U.TimeLineStart,
U.TimeLineEnd, U.UnitsProduced, U.ActLineSpeed, U.TgtLineSpeed, U.OfflineTaskID, R.Rate, S.LabHrsPerThou, S.PHeads, T.TgtOEE, T.TgtEff, T.TgtProd
ORDER BY U.TimeLineStart ASC
聊天中的讨论表明:
- 仅在连接到远程数据库时出现问题。
- 使用旧的 SQLOLEDB 提供程序。
我建议给更新的提供程序 SQLNCLI 一个机会,它在与 MSSQL 通信时应该更有效。修改连接字符串后,执行时间从 2 分钟减少到 3 秒。
当 运行 来自 Excel vba
的查询时,我看到超时错误从 SQL Server Management Studio 查询需要 <2 秒,但从 vba 超时发生在 2 分钟,没有任何返回
我在设置 Command 对象时有没有做错?我注意到 ADODB 似乎更慢,但从来没有像这样
查询连接了几个表并进行了一些其他计算,但是从 1.5 秒到 >2 分钟一定意味着 vba 中的某些内容我错过了
这是我的 vba 连接字符串代码:
If svrCon Is Nothing Then
Set svrCon = New ADODB.Connection
End If
If Not CheckServerConnectionState Then
conStr = "Provider=SQLOLEDB;Data Source=ussantapps332;" & _
"Initial Catalog=Global_OEE_Data_Capture_Dev;User Id=sqluser;Password=*****;"
' Open the connection
svrCon.ConnectionTimeout = 0
svrCon.Open conStr
End If
这是我的vbaSELECT代码:
Dim cmd As ADODB.Command
Dim par As ADODB.Parameter
Dim rst As ADODB.Recordset
' Create command object
Set cmd = New ADODB.Command
cmd.CommandTimeout = 120
cmd.ActiveConnection = svrCon
cmd.CommandText = sql
' Create parameter object
If IsArrayInitialized(params) Then
For x = 0 To UBound(params)
If IsNull(params(x, 1)) Then
Set par = cmd.CreateParameter(Type:=params(x, 0), Size:=1)
Else
Set par = cmd.CreateParameter(Type:=params(x, 0), Size:=Len(params(x, 1)) + 1)
End If
par.Value = params(x, 1)
cmd.Parameters.Append par
Set par = Nothing
DoEvents
Next
End If
' Open recordset object
On Error GoTo ExecuteError
Debug.Print Format(Now, "hh:mm:ss")
Set rst = cmd.Execute
Debug.Print Format(Now, "hh:mm:ss")
On Error GoTo 0
将sql字符串和参数传入函数,连接从另一个方法打开
查询是:
SELECT U.UnitsID, L.LineName, V.VSName, O.OperatorShift, O.LineLeader, O.CotyOps, O.TempOps, U.WorkOrder, U.ProductCode,
S.ProdDesc, U.TimeLineStart, U.TimeLineEnd, U.UnitsProduced, U.ActLineSpeed, U.TgtLineSpeed, SUM(CASE WHEN C.DTIncludedInOEE = 0 THEN D.DowntimeLength ELSE 0 END),
U.OfflineTaskID, R.Rate, S.LabHrsPerThou, S.PHeads, T.TgtOEE, T.TgtEff, T.TgtProd
FROM dataUnits U
LEFT JOIN dataOperatorNames O ON O.OperatorID = U.OperatorNameID
INNER JOIN setupLines L ON U.LineID = L.LineID
INNER JOIN setupValueStreams V on V.VSID = L.VSID
INNER JOIN setupPUs P ON V.PUID = P.PUID
LEFT JOIN dataDowntimes D ON U.UnitsID = D.UnitsID
LEFT JOIN setupDowntimes sD ON D.DTID = sD.DTID
LEFT JOIN setupDowntimeCats C ON sD.DTCatID = C.DTCatID
LEFT JOIN (SELECT VSID, AVG(RateVal) Rate
FROM dataRates WHERE FYStart >= '2014-07-01' AND FYStart < '2015-07-01'
GROUP BY VSID) R ON R.VSID = L.VSID
LEFT JOIN dataStandards S ON S.ProdCode = U.ProductCode
LEFT JOIN (SELECT LineID, AVG(TgtOEE) TgtOEE, AVG(TgtEff) TgtEff, AVG(TgtProd) TgtProd
FROM dataTargets WHERE TgtMonth >= '2015-03-01' AND TgtMonth < '2015-04-01'
GROUP BY LineID) T ON L.LineID = T.LineID
WHERE (S.SAPVersion = (SELECT MIN(SAPVersion) FROM dataStandards s2 WHERE s2.ProdCode = S.ProdCode)
OR S.SAPVersion IS NULL)
AND P.SiteID = 2 AND U.TimeLineStart >= '2015-03-05 23:00' AND U.TimeLineStart < '2015-03-31 23:00'
GROUP BY U.UnitsID, L.LineName, V.VSName, O.OperatorShift, O.LineLeader, O.CotyOps, O.TempOps, U.WorkOrder, U.ProductCode, S.ProdDesc, U.TimeLineStart,
U.TimeLineEnd, U.UnitsProduced, U.ActLineSpeed, U.TgtLineSpeed, U.OfflineTaskID, R.Rate, S.LabHrsPerThou, S.PHeads, T.TgtOEE, T.TgtEff, T.TgtProd
ORDER BY U.TimeLineStart ASC
聊天中的讨论表明:
- 仅在连接到远程数据库时出现问题。
- 使用旧的 SQLOLEDB 提供程序。
我建议给更新的提供程序 SQLNCLI 一个机会,它在与 MSSQL 通信时应该更有效。修改连接字符串后,执行时间从 2 分钟减少到 3 秒。