import java.lang.*; import java.io.*; import java.sql.*; /** The goal of this is is simply read all the rows (someKey, someString) in a database individually. The idea is to test the effect of the ::int8 hack I did on the postgresql JDBC drivers. My hypothesis is that the query optimizer in PostgreSQL is missing the chance to cast things up to INT8's in order to use an existing index. The hack forces this cast. There should be a dramatic time run difference as mine should do an index scan, while the stock JDBC driver will cause a sequential scan. Table should look like this: CREATE TABLE IndexTest ( someKey INT8, someString VARCHAR(1024), PRIMARY KEY (someKey) ); Populate it with items of keys 1-nRow, nRows being defined below. */ public class SelectPerf { private static final long nRows = 1000; public static void main(String[] args) { Connection conn = null; File file = null; FileInputStream fis = null; ByteArrayOutputStream baos = null; long startTime, stopTime; System.out.println("Here we go"); try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql:itest", "kylev", ""); } catch(Throwable t) { System.err.println("Exception during JDBC setup: " + t); System.exit(-1); } System.out.println("We have a connection"); PreparedStatement s = null; try { s = conn.prepareStatement("SELECT someString FROM IndexTest WHERE someKey=?"); } catch (SQLException e) { System.err.println("Exception during prepareStatement: " + e); System.exit(-1); } startTime = System.currentTimeMillis(); for (long l = 1; l <= nRows; l++) { ResultSet rs = null; try { s.setLong(1, l); rs = s.executeQuery(); } catch (SQLException e) { System.err.println("SQLExeption happened during executeQuery: " + e); System.exit(-1); } if (rs != null) { try { rs.close(); // We don't actually care. } catch (SQLException e) { System.err.println("ResultSet didn't want to close: " + e); } } if (l % 100 == 0) System.out.println("Done with " + l + " SELECTs."); } stopTime = System.currentTimeMillis(); System.out.println("Total run time (in millis): " + (stopTime - startTime) ); } }