 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 10th, 2004, 06:52 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
quote:
Sorry for confusing you.
|
Oh. You were referring to index based access to the fields collection of the recordset. You're right - I was confused, but this is a forum about SQL Server and not ASP, so forgive me for vectoring off about the database. :)
Anyway, as I understand it, access to the recordset's field collection is supposed to be marginally faster using an integer index as opposed to the name, which has to be hashed in order to perform the name-based lookup.
You are correct that access by an index is a bit more "fragile" since anybody who messes with the query could change the order of the columns in the resultset and break your index based code. Of course, if they change the name of a column (or alias) that will break your named based code as well.
The fact is that code that is written to refer to or manipulate query results in a recordset is tightly coupled to that query, so any changes to the query results will have an effect on the code, and there is little you can do about that.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

September 10th, 2004, 08:32 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Just one observation if I may. Using index-based access to recordsets makes your code a lot less readable.
Total = rs(12) * rs(17) * (1 - rs(3))
OR
Total = rs("Quantity") * rs("UnitPrice") * (1 - rs("DiscountPercent"))
which would you rather be looking at if you opened up someone else's code?
If you really insist on using index-based methods, I would strongly suggest you take the trouble to define appropriate constants to give others (and yourself when you come back to this code in the future) a chance, example
Const cFieldDiscountPercent=3, cFieldQuantity=12, cFieldUnitPrice=17
...
Total = rs(cFieldQuantity) * rs(cFieldUnitPrice) * (1 - rs(cFieldDiscountPercent))
rgds
Phil
|
|

September 10th, 2004, 08:46 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Yes, Phil is right on that.
I could not read my own code after a while, when used that approach. From the code readablity perspective I would hate to do that. But I wasn't knowing that it[rs(1)] has a better performance than name referencing[rs("FName"]. Quiet interesting.
Cheers!
_________________________
- Vijay G
Strive for Perfection
|
|

September 10th, 2004, 08:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Two points, all of these performance issues are only going to matter if your server is handling very large numbers of queries.
Secondly with the issue of using the ordinal number versus the field name you can always define your own constants to refer to the fields, e.g.
Code:
Const COL_QUANTITY = 12
Const COL_UNIT_PRICE = 17
and then use
if you want the best of both readability and speed.
--
Joe
--
Joe (Co-author Beginning XML, 3rd edition)
|
|

September 10th, 2004, 09:05 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
The index based access to collections in general is marginally better performing than name based. But this is really a micro-optimization. You'll gain far better return on your programming effort by making sure your queries are as efficient as possible and your tables are appropriately structured with the appropriate indexes, etc. - that's where the real performance gains are to be made. One poorly performing query will completely overwhelm the microseconds you might gain by applying your efforts to making your recordset access as efficient as you can. The time would be much better spent analyzing the query and/or database design to remove the inefficiencies there.
I would agree that using constants makes for much more readable (i.e maintainable) code. We use them for just about everything; we try to avoid "magic" numbers in all our code, preferring to use constants in their place. That way, if, say, a query needs to change, then rearranging the field access is a matter of renumbering the constants, all defined in one place, instead of searching for numbers which can be hard to find.
There are an awful lot of microseconds in a bug caused by missing a field reference, after all...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

September 10th, 2004, 09:06 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
One technique I use that (to me at least) argues FOR the index based reference to fields is when I'm building a data tier method that populates a class with DB values:
"SELECT x, y, z FROM entityTable"
...
MyObject.x = objRecordSet(0)
MyObject.y = objRecordSet(1)
MyObject.z = objRecordSet(2)
Admittedly, this results in a little extra work if the query changes, but it seldom does apart from additions to the end which are quick.
Readability is not hurt nearly as much because of the class property names that match to the column indexes.
Ok, enough of my rambling, as Jeff pointed out, this is a SQL thread.
|
|

September 10th, 2004, 10:46 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Before I even keep reading some of the previous posts I stopped at this one:
Quote:
|
quote:I thought if I access them like
|
Quote:
rstEmployee(3)
rstEmployee(4)
|
If you will be using this method, then you are asking for trouble by doing a select * from table.
Declare all of your fields in both and avoid the later headache for your self or the next developer.
Sal
|
|
 |