|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 zhaogang1960 于 2014-3-10 23:41 编辑
在解决使用SQL合并多个工作簿数据时发现:采用顺序连接每个工作簿,用GetRows方法把查询数据写入数组,采用下面两种方式速度上有很大差异,如果不加类似Set rs = cnn.Execute(SQL)语句,即使仅保留连接数据库语句,其他什么也不做,速度也会变得很慢,见第3种情况,原题目见下面链接: 跨工作簿汇总
1、 Set rs = cnn.Execute(SQL) arr = rs.GetRows
- Sub ADO加数组已知工作表名_1() '使用Set rs = cnn.Execute(SQL),arr = rs.GetRows
- tt = Timer
- Dim cnn As Object, rs As Object, SQL$, Mypath$, MyName$, arr, brr(1 To 60000, -1 To 11), i&, j&, m&, n&
- Application.ScreenUpdating = False
- Mypath = ThisWorkbook.Path & ""
- MyName = Dir(Mypath & "*.xls")
- Do While MyName <> ""
- If MyName <> ThisWorkbook.Name Then
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & Mypath & MyName
- SQL = "select * from [Sheet1$a2:l] where 被拆迁人 is not null"
- Set rs = cnn.Execute(SQL)
- arr = rs.GetRows
- For i = 0 To UBound(arr, 2)
- m = m + 1
- brr(m, -1) = m
- For j = 0 To 11
- brr(m, j) = arr(j, i)
- Next
- Next
- End If
- MyName = Dir()
- Loop
- [a1].CurrentRegion.Offset(2).ClearContents
- [a3].Resize(m, 13) = brr
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- MsgBox Timer - tt
- End Sub
复制代码2、 arr = cnn.Execute(SQL).GetRows - Sub ADO加数组已知工作表名_2() 'arr = cnn.Execute(SQL).GetRows
- tt = Timer
- Dim cnn As Object, SQL$, Mypath$, MyName$, arr, brr(1 To 60000, -1 To 11), i&, j&, m&, n&
- Application.ScreenUpdating = False
- Mypath = ThisWorkbook.Path & ""
- MyName = Dir(Mypath & "*.xls")
- Do While MyName <> ""
- If MyName <> ThisWorkbook.Name Then
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & Mypath & MyName
- SQL = "select * from [Sheet1$a2:l] where 被拆迁人 is not null"
- arr = cnn.Execute(SQL).GetRows
- For i = 0 To UBound(arr, 2)
- m = m + 1
- brr(m, -1) = m
- For j = 0 To 11
- brr(m, j) = arr(j, i)
- Next
- Next
- End If
- MyName = Dir()
- Loop
- [a1].CurrentRegion.Offset(2).ClearContents
- [a3].Resize(m, 13) = brr
- cnn.Close
- Set cnn = Nothing
- MsgBox Timer - tt
- End Sub
复制代码3、第一种情况速度很快,第二种情况后者速度很慢,甚至仅保留连接工作簿语句,删除查询语句时,速度也很慢的奇怪现象: - Sub ADO加数组已知工作表名_3() '仅连接工作簿,不做任何操作
- tt = Timer
- Dim cnn As Object, SQL$, Mypath$, MyName$, arr, brr(1 To 60000, -1 To 11), i&, j&, m&, n&
- Application.ScreenUpdating = False
- Mypath = ThisWorkbook.Path & ""
- MyName = Dir(Mypath & "*.xls")
- Do While MyName <> ""
- If MyName <> ThisWorkbook.Name Then
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & Mypath & MyName
- SQL = "select * from [Sheet1$a2:l] where 被拆迁人 is not null"
- ' arr = cnn.Execute(SQL).GetRows
- ' For i = 0 To UBound(arr, 2)
- ' m = m + 1
- ' brr(m, -1) = m
- ' For j = 0 To 11
- ' brr(m, j) = arr(j, i)
- ' Next
- ' Next
- End If
- MyName = Dir()
- Loop
- ' [a1].CurrentRegion.Offset(2).ClearContents
- ' [a3].Resize(m, 13) = brr
- cnn.Close
- Set cnn = Nothing
- MsgBox Timer - tt
- End Sub
复制代码
请看附件
SQL顺序连接多个工作簿速度的疑问.rar
(305.58 KB, 下载次数: 213)
Excel2007或2010附件:
SQL顺序连接多个工作簿速度的疑问20072010.rar
(443.85 KB, 下载次数: 227)
该贴已经同步到 zhaogang1960的微博
|
|