Comparing the performance of a result set that retrieves data using an index and column name

Will there be a performance difference when I use an index to retrieve data with a column name, and I'm talking about doing this operation millions of times a day on the server.

rs.getString(1) vs rs.getString("columnname");

EDIT: JDBC Version Oracle JDBC Driver 10.2.0.4.0

+9
java resultset
source share
2 answers

rs.getString(n); will work a little faster because it retrieves directly from the collection, not the search.

Hundreds of future readers of your code will appreciate rs.getString("columnname"); instead of looking for SQL to see what index n means.

+11
source share

It does not really matter. A hit in the database will be many times slower than accessing column values.

rs.getString(n) will be negligible faster. However, this will depend on the driver implementation and the number of columns in the result. Most implementations are likely to use HashMap to map column names to an index, but not necessarily. In addition, some drivers can create a HashMap lazily, which means the first row will be the slowest to access by column name. JTDS, for example, performs a linear search for columns that are not already in the HashMap.

EDIT: minor changes and regrouping. No content changes.

+4
source share

All Articles