Program | Performance ten, which method of referencing recordset field value is the most efficient?
10.1 Test
So far we've been referencing field values in a recordset by name. Because this method requires every time to find the appropriate field, it is not efficient. To prove this, in the following test we refer to its value (ado__08.asp) by the index of the field in the collection:
' Write Data
Do as Not objrs.eof
Response.Write (_
"< TR >" & _
"< TD >" & objRS (0) & "</td >" & _
"< TD >" & objRS (1) & "</td >" & _
"< TD >" & objRS (2) & "</td >" & _
"< TD >" & objRS (3) & "</td >" & _
"< TD >" & objRS (4) & "</td >" & _
"< TD >" & objRS (5) & "</td >" & _
"< TD >" & objRS (6) & "</td >" & _
"</tr >" _
)
Objrs.movenext
Loop
As expected, there is a slight change in the cost of the page (perhaps because the code is slightly reduced). However, the improvement in the display time of this method is quite obvious.
In the next test, we bind all the fields to the variable (ado__09.asp):
If objrs.eof Then
Response.Write ("No Records Found")
Else
' Write headings
...
Dim fld0
Dim Fld1
Dim Fld2
Dim fld3
Dim Fld4
Dim Fld5
Dim Fld6
Set fld0 = objRS (0)
Set fld1 = objRS (1)
Set fld2 = objRS (2)
Set fld3 = objRS (3)
Set fld4 = objRS (4)
Set fld5 = objRS (5)
Set Fld6 = objRS (6)
' Write Data
Do as Not objrs.eof
Response.Write (_
"< TR >" & _
"< TD >" & fld0 & "</td >" & _
"< TD >" & fld1 & "</td >" & _
"< TD >" & fld2 & "</td >" & _
"< TD >" & fld3 & "</td >" & _
"< TD >" & fld4 & "</td >" & _
"< TD >" & fld5 & "</td >" & _
"< TD >" & fld6 & "</td >" & _
"</tr >" _
)
Objrs.movenext
Loop
Set fld0 = Nothing
Set fld1 = Nothing
Set fld2 = Nothing
Set fld3 = Nothing
Set Fld4 = Nothing
Set fld5 = Nothing
Set Fld6 = Nothing
Response.Write ("</table >")
End If
This is the best record so far. Note that the display time for a single record has been lowered below 0.45 milliseconds.
The above script requires an understanding of the construction of the result recordset. For example, we used the field names directly in the column headings to refer to each field value individually. In the following test, not only is the field data obtained by traversing the field collection, but also the field headings are obtained in the same way, which is a more dynamic scheme (ado__10.asp).
If objrs.eof Then
Response.Write ("No Records Found")
Else
' Write headings
Response.Write ("< TABLE border=1 >< TR >")
For each objfld in Objrs.fields
Response.Write ("< TH >" & objfld.name & "</th >")
Next
Response.Write ("</tr >")