I ran this performance test showing that using a PreparedStatement was roughly 2.5x - 3.0x faster than creating the query each time:
private static void testQueries(Connection connection)
throws SQLException {
int numberOfRuns = 50;
//try regular statements - created and closed each time
long startTime = System.currentTimeMillis();
for(int i = 0; i < numberOfRuns; i++) {
ResultSet resultSet = executeQuery(connection,
"select ct.new_account_number "
+ "from dang0.customertypes@cmsm ct, dang0.customers@cmsm c "
+ "where ct.account_number = c.account_number "
+ "and c.status = 'A' "
+ "and rownum <= 100");
resultSet.getStatement().close();
}
System.out.println("Regular statements took " + (System.currentTimeMillis() - startTime));
//try creating and deleting preparedStatements
startTime = System.currentTimeMillis();
for(int i = 0; i < numberOfRuns; i++) {
PreparedStatement preparedStatement = connection.prepareStatement(
"select ct.new_account_number "
+ "from dang0.customertypes@cmsm ct, dang0.customers@cmsm c "
+ "where ct.account_number = c.account_number "
+ "and c.status = 'A' "
+ "and rownum <= ?");
preparedStatement.setInt(1,100);
ResultSet resultSet = preparedStatement.executeQuery();
preparedStatement.close();
}
System.out.println("Prepared statements took " + (System.currentTimeMillis() - startTime));
//try reusing 1 prepared statement
startTime = System.currentTimeMillis();
PreparedStatement preparedStatement
= createAndStorePreparedStatement("com.mpowercom.util.SQLTools.test",
connection,
"select ct.new_account_number "
+ "from dang0.customertypes@cmsm ct, dang0.customers@cmsm c "
+ "where ct.account_number = c.account_number "
+ "and c.status = 'A' "
+ "and rownum <= ?");
for(int i = 0; i < numberOfRuns; i++) {
preparedStatement.setInt(1,100);
ResultSet resultSet = preparedStatement.executeQuery();
}
preparedStatement.close();
System.out.println("1 Prepared execute took " + (System.currentTimeMillis() - startTime));
}
and got:
Regular statements took 27410
Prepared statements took 27570
1 Prepared execute took 9340