views:

97

answers:

3

When iterating through a Resultset in the standard way

while (rs.next()){
...
}

To retrieve column data (e.g long) is it quicker to use

rs.getLong(String columnLabel)

or

rs.getLong(int columnIndex)

Presumably columnLabel is better to use in many ways for stronger code, but is there a significant operational loss by matching a column to the string every time (we're talking table sizes of ~40m rows)?

A: 

I'd suggest that you think more about what's clearer than what's quicker.

Here's an example: Say you have a query, select * from .... and when you iterate over the ResultSet, you're calling String a = resultSet.getString(1), int i = resultSet.getInt(2) etc etc

Then you change your table definition so that you have some more columns... all of a sudden all your gets are going to break because you referenced everything by int. If you'd referenced by column name, then there would be no break.

To me this seems a lot clearer than referencing by column number. You'll also save a lot of time later on when trying to debug your old code to figure out why it's breaking.

Noel M
Your example illustrates that you should list the attributes selected in the order you need them instead of relying on `select * from`. Using an index to retrieve those values is less of a maintenance problem because you specified the sequence.
rsp
@Noel: Definitely agree. This is the point I inelegantly smoothed over with the line "better to use in many ways for stronger code".
sawu
A: 

Depends entirely on the internal structure. If they operate any sort of cache, it won't be a big deal. If they use a hashmap, it's unlikely to be substantial. A system engineered to support this won't have too much trouble coping. However, you should really think about what makes more sense in the context of your code, and come back to this if a profile demonstrates performance problems.

DeadMG
+3  A: 

I'd wager that compared to the cost of preparing the result set the difference is negligable. Favour the robust code.

djna