admin管理员组文章数量:1401650
The code below has worked very well for the last 6 or 7 years. The database is SQL Server 2008 R2, the client is Excel 365 64-bit (v.2502). Over the past few months, some users are seeing execution times as high as 8 minutes when 45 seconds is more common for a query returning 250K records. Standard speed optimizations are in place: calculation is set to manual and screenupdating is off.
Sub QueryExecute(rngDta As Range, strCnn As String, strSQL As String)
Dim cnnADO As New ADODB.Connection
Dim rstADO As New ADODB.Recordset
cnnADO.Open strCnn
cnnADO.CommandTimeout = 600 '10 minutes.
rstADO.CursorLocation = adUseClient 'Client-side curser allows use of RecordCount property.
rstADO.Open strSQL, cnnADO, adOpenForwardOnly, adLockReadOnly
Set rstADO.ActiveConnection = Nothing 'Close connection to server.
If Not (rstADO.BOF Or rstADO.EOF) Then 'If rows returned.
rngDta.CopyFromRecordset data:=rstADO
End If
rstADO.Close
cnnADO.Close
Set rstADO = Nothing
Set cnnADO = Nothing
End Sub
I had been looking at server or network latency, but have discovered that the CopyFromRecordset method appears to be the problem. What is very strange is that when stepping through the code in debug mode, if I execute the CopyFromRecordset line with the F8 key, it takes 19 seconds to populate the worksheet range. If I just run it normally, it can take 7 minutes (according to the debug.print statements).
What would explain the huge time difference between the two execution methods?
Thanks!
版权声明:本文标题:sql - Excel CopyFromRecordset method has suddenly gotten very slow but still fast in debug mode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744227064a2596143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论