|
楼主 |
发表于 2014-12-17 16:42
|
显示全部楼层
测试发现2003和2010测试的结论是一致的,所以下面只述及用2003的情况.
在附件中有一个文件名为:自连接测试.xls的工作簿,该工作簿中有一个数据源表,表名为:明细表
另外有一个工作簿123.xls,其中也有一个与上面明细表数据完全相同的明细表.作为比较之用.
在自连接测试.xls工作簿中有两个功能相同代码段TEST1与TEST2,一个是自连接,一个不是.都循环重复执行100次:- Sub TEST1()
- Dim intRow As Integer, t As Single, sql As String
- t = Timer
- For i = 1 To 100
- 清空数据
- Set cnn = CreateObject("adodb.connection")
- intRow = Sheets("明细表").Range("A65536").End(xlUp).Row
- cnn.Open "provider=Microsoft.ace.OLEDB.12.0;Extended Properties=""excel 8.0;hdr=yes"";Data Source=" & ThisWorkbook.Path & "\123.xls"
- sql = "select * from [明细表$A1:D" & intRow & "] where 地点 Like '%" & Range("d2") & "%'"
- sql = sql & " and 时间 between #" & Range("b2") & "#and#" & Range("b3") & "# and 名称='" & Range("d3") & "'"
- Sheet1.Range("A6").CopyFromRecordset cnn.Execute(sql, 0, 1)
- cnn.Close
- Set cnn = Nothing
- Next
- MsgBox "TEST(1)100次时间: " & Timer - t
- End Sub
- Sub TEST2()
- Dim intRow As Integer, t As Single, sql As String
- t = Timer
- For i = 1 To 100
- 清空数据
- Set cnn = CreateObject("adodb.connection")
- intRow = Sheets("明细表").Range("A65536").End(xlUp).Row
- cnn.Open "provider=Microsoft.ace.OLEDB.12.0;Extended Properties=""excel 8.0;hdr=yes"";Data Source=" & ThisWorkbook.FullName
- sql = "select * from [明细表$A1:D" & intRow & "] where 地点 Like '%" & Range("d2") & "%'"
- sql = sql & " and 时间 between #" & Range("b2") & "#and#" & Range("b3") & "# and 名称='" & Range("d3") & "'"
- Sheet1.Range("A6").CopyFromRecordset cnn.Execute(sql, 0, 1)
- cnn.Close
- Set cnn = Nothing
- Next
- MsgBox "TEST(2)100次时间: " & Timer - t
- End Sub
- Sub 清空数据()
- If Range("A6") <> "" Then
- Range("A6:D" & [A65536].End(xlUp).Row).ClearContents
- End If
- End Sub
复制代码 |
|