Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

resultset

Would there be a difference in performance when i use index to get data vs column name and I am talking about doing this operation a millions of times a day in the server.

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

EDIT: JDBC version Oracle JDBC driver 10.2.0.4.0

like image 604
Srujan Kumar Gulla Avatar asked Dec 31 '12 16:12

Srujan Kumar Gulla


2 Answers

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.

like image 120
Gilbert Le Blanc Avatar answered Oct 19 '22 20:10

Gilbert Le Blanc


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.

like image 22
mikeslattery Avatar answered Oct 19 '22 20:10

mikeslattery