检索记录集时,应用程序使用Bang(!)运算符访问记录集Eg RS!OrderId中的字段.
虽然我知道这种做法,但我从未真正使用过它(除非我一直懒惰),我也没有使用RS(“OrderId”),因为我一直(或通常)使用完全合格的方法(例如RS. fields(“OrderId”).value.甚至使用.Item属性进一步扩展它.)
两者都返回完全相同的值,一个比另一个更短.
我坚持这种方法的原因是,在遥远的过去的某个时候,我相信我被告知完全限定字段的性能更高,因为代码必须翻译每次出现的!运算符到其完全合格的姐妹.然而 !运算符减少了打字和这样的开发时间.
我似乎也记得那个! ADO将在未来某个时候被弃用.但它仍然似乎在代码中我只是想知道哪种方法被认为是最佳实践,哪种方法比另一方更好.
我已经彻底测试了VB6和ADO的性能,以便在我的应用程序中使用.从记录集中获取数据的绝对最快方法是使用FIELD对象.返回大量行时,您会注意到性能的巨大差异.以下是我的应用程序的代码块(缩小以突出正确使用字段对象).Dim fMinLongitude As ADODB.Field Dim fMinLatitude As ADODB.Field Dim fMaxLongitude As ADODB.Field Dim fMaxLatitude As ADODB.Field Dim fStreetCount As ADODB.Field If RS.RecordCount = 0 Then Exit Sub End If Set fMinLongitude = RS.Fields.Item("MinLongitude") Set fMinLatitude = RS.Fields.Item("MinLatitude") Set fMaxLongitude = RS.Fields.Item("MaxLongitude") Set fMaxLatitude = RS.Fields.Item("MaxLatitude") Set fStreetCount = RS.Fields.Item("StreetCount") While Not RS.EOF LineGridCount = LineGridCount + 1 With LineGrid(LineGridCount) .MinLongitude = fMinLongitude.Value .MaxLongitude = fMaxLongitude.Value .MinLatitude = fMinLatitude.Value .MaxLatitude = fMaxLatitude.Value End With RS.MoveNext Wend RS.Close Set RS = Nothing
请注意,我为SQL Server存储过程返回的5列设置了字段对象.然后我在循环中使用它们.当您执行RS.MoveNext时,它会影响字段对象.
使用上面显示的代码,我可以在不到1秒的时间内将26,000行加载到用户定义的类型中.事实上,通过代码运行需要0.05秒.在编译的应用程序中,它甚至更快.
如果不使用字段对象,则至少应使用WITH块.正如另一篇文章中所提到的,使用序数位置比其他替代方案更快(除了场方法).如果您打算使用序号位置,那么您应该使用WITH块.例如:
With RS.Fields ID = .Item(0).Value Name = .Item(1).Value EyeColor = .Item(2).Value End With
使用with块很不错,因为它减少了键入的数量,同时加快了代码的执行速度.之所以出现这种性能提升,是因为VB可以设置一次指向字段对象的指针,然后在每次调用fields对象时重用该指针.
顺便说一下……我不喜欢“少打字”的说法.我经常发现性能更好的代码也是更复杂的代码.使用VB6的intellisense,额外的输入也不是那么多.
RS(“FieldName”)是15个字符.
我已经习惯了输入:rs(dot)f(dot)i(左括号)(引用)FieldName(引用)(Close括号)(点)v.这是6个额外的按键用于完全使用合格的方法.
使用with block方法,它将是(点)i(左括号)(引用)FieldName(引用)(右括号)(点)v,这是17次按键.
这是一种良好的习惯需要很少的努力并且通过具有更好的代码来获得巨大回报的情况之一.
我刚做了一些性能测试.以下测试使用客户端游标,这意味着查询返回的所有数据都将复制到客户端计算机并存储在记录集对象中.
我用于性能测试的代码是这样的:
Private Sub Command1_Click() Dim DB As ADODB.Connection Dim RS As ADODB.Recordset Dim Results() As String Set DB = New ADODB.Connection DB.ConnectionString = "my connection string here" DB.CursorLocation = adUseClient DB.Open Set RS = New ADODB.Recordset Call RS.Open("Select * From MapStreetsPoints", DB, adOpenForwardOnly, adLockReadOnly) Dim Start As Single Dim FeatureId As Long Dim PointNumber As Long Dim Longitude As Single Dim Latitude As Single Dim fFeatureId As ADODB.Field Dim fPointNumber As ADODB.Field Dim fLongitude As ADODB.Field Dim fLatitude As ADODB.Field ReDim Results(5) RS.MoveFirst Start = Timer Do While Not RS.EOF FeatureId = RS!FeatureId PointNumber = RS!PointNumber Longitude = RS!Longitude Latitude = RS!Latitude RS.MoveNext Loop Results(0) = "Bang Method: " & Format(Timer - Start, "0.000") RS.MoveFirst Start = Timer Do While Not RS.EOF FeatureId = RS.Fields.Item("FeatureId").Value PointNumber = RS.Fields.Item("PointNumber").Value Longitude = RS.Fields.Item("Longitude").Value Latitude = RS.Fields.Item("Latitude").Value RS.MoveNext Loop Results(1) = "Fully Qualified Name Method: " & Format(Timer - Start, "0.000") RS.MoveFirst Start = Timer Do While Not RS.EOF FeatureId = RS.Fields.Item(0).Value PointNumber = RS.Fields.Item(1).Value Longitude = RS.Fields.Item(2).Value Latitude = RS.Fields.Item(3).Value RS.MoveNext Loop Results(2) = "Fully Qualified Ordinal Method: " & Format(Timer - Start, "0.000") RS.MoveFirst Start = Timer With RS.Fields Do While Not RS.EOF FeatureId = .Item("FeatureId").Value PointNumber = .Item("PointNumber").Value Longitude = .Item("Longitude").Value Latitude = .Item("Latitude").Value RS.MoveNext Loop End With Results(3) = "With Block Method: " & Format(Timer - Start, "0.000") RS.MoveFirst Start = Timer With RS.Fields Do While Not RS.EOF FeatureId = .Item(0).Value PointNumber = .Item(1).Value Longitude = .Item(2).Value Latitude = .Item(3).Value RS.MoveNext Loop End With Results(4) = "With Block Ordinal Method: " & Format(Timer - Start, "0.000") RS.MoveFirst Start = Timer Set fFeatureId = RS.Fields.Item("FeatureId") Set fPointNumber = RS.Fields.Item("PointNumber") Set fLatitude = RS.Fields.Item("Latitude") Set fLongitude = RS.Fields.Item("Longitude") Do While Not RS.EOF FeatureId = fFeatureId.Value PointNumber = fPointNumber.Value Longitude = fLongitude.Value Latitude = fLatitude.Value RS.MoveNext Loop Results(5) = "Field Method: " & Format(Timer - Start, "0.000") Text1.Text = "Rows = " & RS.RecordCount & vbCrLf & Join(Results, vbCrLf) End Sub
结果是:
Rows = 2,775,548 Bang Method: 9.441 Fully Qualified Name Method: 9.367 Fully Qualified Ordinal Method: 5.191 With Block Method: 8.527 With Block Ordinal Method: 5.117 Field Method: 4.316
显然,现场方法是胜利者.它需要的时间不到爆炸方法的1/2.还要注意,与场方法相比,序数方法也具有不错的性能.