Using get methods effectively
- Last Updated: August 6, 2018
- 1 minute read
- DataDirect Connectors
- JDBC
- Aha! 6.0
- Amazon Redshift 6.0
- Apache Cassandra 6.0
- Apache Hive 6.0
- Apache Spark SQL 6.0
- Atlassian Jira 6.0
- Autonomous REST Connector 6.0
- Cloudera Impala 5.1
- + 24
JDBC provides a variety of methods to return data from a result set (for example, getInt(), getString(), and getObject()). The getObject() method is the most generic and provides the worst performance when the non-default mappings are specified because the JDBC driver must perform extra processing to determine the type of the value being returned and generate the appropriate mapping. Always use the specific method for the data type.
To further improve performance, provide the column number of the column being returned, for
example, getString(1), getLong(2), and
getInt(3), instead of the column name. If the column names are not
specified, network traffic is unaffected, but costly conversions and lookups increase. For
example, suppose you use:
getString("foo")...
The JDBC driver may need to convert foo to uppercase and then compare foo with all columns in the column list, which is costly. If the driver is able to go directly to result column 23, a large amount of processing is saved.
For example, suppose you have a result set that has 15 columns and 100 rows, and the column
names are not included in the result set. You are interested in only three columns:
EMPLOYEENAME (string), EMPLOYEENUMBER (long integer), and SALARY (integer). If you specify
getString("EmployeeName"), getLong("EmployeeNumber"), and
getInt("Salary"), each column name must be converted to the appropriate
case of the columns in the database metadata and lookups would increase considerably.
Performance improves significantly if you specify getString(1),
getLong(2), and getInt(15).