Ask a Question related to ASP Database, Design and Development.
-
John Beschler #1
Best way to access field values using ADO.
Is there any difference, performance-wise, between these
two methods?
Method 1:
szSQL = "SELECT fld1, fld2, fld3 FROM tbl1"
Set objRS = objConn.Execute(szSQL)
....
szFld1 = objRS("fld1")
szFld2 = objRS("fld2")
szFld3 = objRS("fld3")
Method 2:
Const cFld1 = 0
Const cFld2 = 1
Const cFld3 = 2
szSQL = "SELECT fld1, fld2, fld3 FROM tbl1"
Set objRS = objConn.Execute(szSQL)
....
szFld1 = objRS(cFld1)
szFld2 = objRS(cFld2)
szFld3 = objRS(cFld3)
I've read many places to avoid the first method because
ADO must do a lookup to retrieve the ordinal positions of
the fields anyway. However, in the second method, doesn't
it still have to do a lookup to get the value for the
constants? I guess the difference is the first method has
to query the database for the ordinal position where the
second method just has to look in memory. Is this correct?
Thanks,
John
John Beschler Guest
-
Getting values from a field in a pdf
I have written the following piece of code to get the value from a field in a pdf. ASTCount* nLength; AVDoc myAVDoc = AVAppGetActiveDoc();... -
#38458 [Asn->Csd]: Fails to get values of fields following a MEMO type field in MS Access
ID: 38458 Updated by: wez@php.net Reported By: costas at meezon dot com -Status: Assigned +Status: ... -
#38458 [Com]: Fails to get values of fields following a MEMO type field in MS Access
ID: 38458 Comment by: johnc at inkjetinc dot com Reported By: costas at meezon dot com Status: Assigned Bug... -
Using Field Values in If/Then/Else Statements
I need to know how to use the value of a table field in a If...Then...Else... statement. I'm using an expression to merge data from several fields... -
Access auto fill/populate field with list of different values
I am creating a database with the following fields in the table ID - ID Number for person LAB - Lab Name LABDT - Lab date LABVAL - Value of the... -
Bob Barrows #2
Re: Best way to access field values using ADO.
John Beschler wrote:
Yes, but unless you're looping, you are unlikely to notice the difference.> Is there any difference, performance-wise, between these
> two methods?
>
Oh! Which is faster? As a general rule, it is always faster to reference
items in a collection by their index number vs their name, so Method 2 will
be faster.
However, you haven't mentioned the fastest method: using Field objects
Dim oFld1, oFld2, oFld3
Const cFld1 = 0
Const cFld2 = 1
Const cFld3 = 2
szSQL = "SELECT fld1, fld2, fld3 FROM tbl1"
Set objRS = objConn.Execute(szSQL)
....
SET oFld1= objRS(cFld1)
SET oFld1 = objRS(cFld2)
SET oFld1 = objRS(cFld3)
'then, in a loop, reference the field objects' value property
I would not use this method unless I was looping through a lot of records,
something which I am unlikely to be doing in asp except via a GetRows array.
No, the benefit of using constants is that they are resolved at> I've read many places to avoid the first method because
> ADO must do a lookup to retrieve the ordinal positions of
> the fields anyway. However, in the second method, doesn't
> it still have to do a lookup to get the value for the
> constants?
compile-time. When the compiler compiles this code, it substitutes the value
of the constant for every occurrence of the constant in the code. So when
the code is executed at run-time, the code that runs is
szFld1 = objRS(0)
not
szFld1 = objRS(cFld1)
HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Bob Barrows Guest
-
John Beschler #3
Re: Best way to access field values using ADO.
Ya learn something new everyday.
I'd never heard of field objects. Thanks for the pointer.
I am trying to use GetRows in any new stuff that I do;
however, when maintaining existing code it seems to me to
be easier to change the field references from rs
("somefield") to rs(cConstant) than recoding the whole
thing using getrows.
Thanks again. You guys are so smart!
John
notice the difference.>-----Original Message-----
>John Beschler wrote:>>> Is there any difference, performance-wise, between these
>> two methods?
>>
>Yes, but unless you're looping, you are unlikely tofaster to reference>
>Oh! Which is faster? As a general rule, it is alwaysname, so Method 2 will>items in a collection by their index number vs theirField objects>be faster.
>
>However, you haven't mentioned the fastest method: usingproperty>
>
>Dim oFld1, oFld2, oFld3
>Const cFld1 = 0
>Const cFld2 = 1
>Const cFld3 = 2
>szSQL = "SELECT fld1, fld2, fld3 FROM tbl1"
>Set objRS = objConn.Execute(szSQL)
>....
>SET oFld1= objRS(cFld1)
>SET oFld1 = objRS(cFld2)
>SET oFld1 = objRS(cFld3)
>'then, in a loop, reference the field objects' valuea lot of records,>
>I would not use this method unless I was looping throughvia a GetRows array.>something which I am unlikely to be doing in asp exceptof>>> I've read many places to avoid the first method because
>> ADO must do a lookup to retrieve the ordinal positionsdoesn't>> the fields anyway. However, in the second method,resolved at>>> it still have to do a lookup to get the value for the
>> constants?
>No, the benefit of using constants is that they aresubstitutes the value>compile-time. When the compiler compiles this code, itthe code. So when>of the constant for every occurrence of the constant inin my From>the code is executed at run-time, the code that runs is
>
>szFld1 = objRS(0)
>
>not
>
>szFld1 = objRS(cFld1)
>
>HTH,
>Bob Barrows
>
>--
>Microsoft MVP -- ASP/ASP.NET
>Please reply to the newsgroup. The email account listedYou will get a>header is my spam trap, so I don't check it very often.>quicker response by posting to the newsgroup.
>
>
>.
>John Beschler Guest
-
Aaron Bertrand - MVP #4
Re: Best way to access field values using ADO.
> I am trying to use GetRows in any new stuff that I do;
As Bob suggested, this modification is probably for nothing; you are> however, when maintaining existing code it seems to me to
> be easier to change the field references from rs
> ("somefield") to rs(cConstant) than recoding the whole
> thing using getrows.
unlikely to see any performance gains here. So I don't see why it's worth
the effort...
--
Aaron Bertrand
SQL Server MVP
[url]http://www.aspfaq.com/[/url]
Aaron Bertrand - MVP Guest
-
Bob Barrows #5
Re: Best way to access field values using ADO.
Aaron Bertrand - MVP wrote:
Exactly. Change the way you code your new applications. Don't bother messing>>> I am trying to use GetRows in any new stuff that I do;
>> however, when maintaining existing code it seems to me to
>> be easier to change the field references from rs
>> ("somefield") to rs(cConstant) than recoding the whole
>> thing using getrows.
> As Bob suggested, this modification is probably for nothing; you are
> unlikely to see any performance gains here. So I don't see why it's
> worth the effort...
with the old ones.
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Bob Barrows Guest
-
Aaron Bertrand - MVP #6
Re: Best way to access field values using ADO.
> Exactly. Change the way you code your new applications. Don't bother
messingThough, if he's in there fixing other things, changing to GetRows() would be> with the old ones.
a much more valuable transition than adding those CONST declarations and
cetera.
--
Aaron Bertrand
SQL Server MVP
[url]http://www.aspfaq.com/[/url]
Aaron Bertrand - MVP Guest
-
John Beschler #7
Re: Best way to access field values using ADO.
OK, Now I'm really confused. Are you saying there's no
discernable difference between rs("Somefieldname") and rs
(0)?
What started this whole thing, is I did a search on our
intranet web site for "SELECT *" and there's a bunch of
them. I was going to go through and start eliminating
those when I have some spare time. In the process, I
thought I'd fix the Field references as well.
Am I wasting my time?
Don't bother>-----Original Message----->> Exactly. Change the way you code your new applications.GetRows() would be>messing>>> with the old ones.
>Though, if he's in there fixing other things, changing todeclarations and>a much more valuable transition than adding those CONST>cetera.
>
>--
>Aaron Bertrand
>SQL Server MVP
>[url]http://www.aspfaq.com/[/url]
>
>
>.
>John Beschler Guest
-
Bob Barrows #8
Re: Best way to access field values using ADO.
John Beschler wrote:
Not unless you are looping, and even then, it will take quite a few loops,> OK, Now I'm really confused. Are you saying there's no
> discernable difference between rs("Somefieldname") and rs
> (0)?
>
and quite a few field value reads in that loop before you will detect the
difference. You can test it yourself and see. You might want to create a VBA
routine to test it so you can use the GetTickCount API method to get a very
accurate time measurement.
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Bob Barrows Guest
-
Aaron Bertrand [MVP] #9
Re: Best way to access field values using ADO.
> OK, Now I'm really confused. Are you saying there's no
I'd be surprised if a human could time the difference.> discernable difference between rs("Somefieldname") and rs
> (0)?
This is a different issue altogether! SELECT * is definitely a higher> What started this whole thing, is I did a search on our
> intranet web site for "SELECT *" and there's a bunch of
> them.
performance burden (over naming explicit columns) than the difference
between rs("name") and rs(ordinal). And this certainly wouldn't be wasted
effort.
Even better would be to move all your ad hoc SELECT statements into stored
procedures / stored queries.
--
Aaron Bertrand
SQL Server MVP
[url]http://www.aspfaq.com/[/url]
Aaron Bertrand [MVP] Guest
-
John Beschler #10
Re: Best way to access field values using ADO.
statements into stored>Even better would be to move all your ad hoc SELECTI have actually seen some very significant performance>procedures / stored queries.
improvement when moving adhoc SQL into a store procedure
(we are running SQL 2000), so I understand the benefits
there; however, do you recommend a stored prcedure for
every function on the web site?
For example: Let's say on one page I need Name, address,
Phonenumber. SO I code a SP to return those three fields.
Set objRS = objConn.Execute MySP MyParams....
On another page I may need only Name, and Phonenumber. Do
I write another SP for that page?
Set objRS = objConn.Execute MyOtherSP MyOtherParams
I guess from a maintenance standpoint that would be
better, cinse there would be no worries that changing a
stored procedure for one page would break it for another.
What are your thoughts on this?
Thanks,
John
>
>--
>Aaron Bertrand
>SQL Server MVP
>[url]http://www.aspfaq.com/[/url]
>
>
>.
>John Beschler Guest
-
Aaron Bertrand - MVP #11
Re: Best way to access field values using ADO.
> (we are running SQL 2000), so I understand the benefits
We control *all* data access through stored procedures; it is simply a> there; however, do you recommend a stored prcedure for
> every function on the web site?
policy. Why would you want to allow any ad hoc queries? Keep your data
access consistent; use stored procedures everywhere. Yes, for all functions
in a web site. If you are accessing your data, you owe it to yourself to
access it as properly, consistently and efficiently as possible.
This is a judgment call, and depends more on how much data is coming back> For example: Let's say on one page I need Name, address,
> Phonenumber. SO I code a SP to return those three fields.
>
> Set objRS = objConn.Execute MySP MyParams....
>
> On another page I may need only Name, and Phonenumber. Do
> I write another SP for that page?
'wasted'... is this stored procedure returning one row, page 1 of 10, or the
whole table? Typically, we have a single stored procedure that returns all
attributes of a single entity (as opposed to all entities - usually when
returning all entities we only need the internal and external identifier,
e.g. the primary key and the label). If we get a couple of extra columns on
certain pages, it's really not that big a deal.
--
Aaron Bertrand
SQL Server MVP
[url]http://www.aspfaq.com/[/url]
Aaron Bertrand - MVP Guest



Reply With Quote

