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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With