Performance comparison of result set extracting data using index vs column name

The rs.getString(n); will perform slightly faster, because it's retrieving directly from a collection, rather than searching.

Hundreds of future readers of your code will appreciate the rs.getString("columnname"); rather than having to look up the SQL to see what the index n refers to.


It doesn't really matter. The hit to the database will be many many times slower than accessing the column values.

rs.getString(n) will be negligibly faster. However, it's going to depend on the driver implementation and number of columns in the result. Most implementations will likely use a HashMap to map column names to an index, but not necessarily. Also, some drivers may build the HashMap lazily which means the first row will be the slowest to access by column name. JTDS, as an example, does a linear search for columns not yet in its HashMap.

EDIT: minor edits and rearranged. No content change.

Tags:

Java

Resultset