【发布时间】:2009-12-14 03:39:23
【问题描述】:
如何通过批处理或其他技术优化速度?我正在从 Excel 2003 VBA 中搜索它的 20MB Access2003 数据库。
我有我的访问表键控(自动编号)所以我虽然这将提供智能非线性搜索,如二进制搜索。目前从 147k 条记录的表中搜索 4000 个值需要 4.2 分钟。
我在搜索中找到了这个:
在 SQL Server 端直接 SELECT 的问题是,除非您正在使用的列上有索引,否则数据库将对表进行线性搜索;那么数据库可以更智能。StackOverflow SQL C# Binary Search Question
这是真的吗?它是否也适用于 Access2003 DB?
VBA 代码,示例:
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection 'open the connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open "PNdb2003.mdb"
End With
'define the record set
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient 'for smaller datasets that fit into RAM
For Each myVariant In Selection.Cells
strSearchText = myVariant
Dim sSQL As String
sSQL = "SELECT Key FROM [MasterTable] WHERE PN=""" & strSearchText & """"
rst.Open Source:=sSQL, ActiveConnection:=cnn, CursorType:=adOpenStatic, LockType:=adLockOptimistic
Cells(myVariant.Row, 7).CopyFromRecordset rst
rst.Close
Next myVariant
cnn.Close
【问题讨论】: