用于在Microsoft Access SQL中优化搜索的索引字符串

时间:2013-10-19 09:08:39

标签: ms-access ms-access-2010

我有一个带有2.000.000行的Microsoft Access文件(.mdb)并且我正在使用查询:

select count(*) from tblABC where instr(Column1 , "something") > 0

让所有记录都包含“某事”这个词。然而,在返回结果之前查询冻结了40秒,这对我来说很糟糕,因为我通常需要这样做。如何通过为其提供与其他数字列相似的索引来优化搜索?

1 个答案:

答案 0 :(得分:1)

根据上述评论的建议,LIKE "*...*"似乎确实比InStr(...) > 0快得多。当我在具有一百万行代码的表上测试它时

Sub SearchTest()
Dim t0 As Single, strWhere As String
t0 = Timer
strWhere = "InStr(TextField, ""86753"") > 0"
Debug.Print DCount("*", "MillionRows", strWhere) & " row(s) found"
Debug.Print "Search took " & Format(Timer - t0, "0.0") & " seconds"
End Sub

报告

20 row(s) found
Search took 5.4 seconds

当我将搜索字符串更改为

strWhere = "TextField LIKE ""*86753*"""

报道了

20 row(s) found
Search took 3.4 seconds

我还在[TextField]上使用和不使用索引对其进行了测试,并且索引对我的结果没有任何影响。但是,[TextField]也是唯一的(" Item0000001"," Item0000002",..." Item1000000"),因此该字段上的索引可能如果该字段包含不同的数据(尽管我不打赌),它会产生影响。