Skip to content

Potential optimizations

Nic Hartley edited this page Nov 18, 2018 · 1 revision

PreparedStatements instead of raw strings for queries

Put this somewhere it's used once:

final Connection connection = DriverManager.getConnection(
            "jdbc:postgresql://localhost:5432/postgres?user=USERNAME&password=PASSWORD");
final PreparedStatement ps =
            connection.prepareStatement("SELECT supporter, survey_data FROM supports WHERE exhibit = ?");

This can be part of a method call that is done frequently:

ps.setLong(1, eid);
ResultSet rs = ps.executeQuery();
List<Survey> responses = new LinkedList<>();
while (rs.next()) {
    responses.add(new Survey(rs));
}
return responses;

This resulted in 3x the speed for the queries alone in a loop, and overall 15 -> 12ms average response time over several trials.

This isn't a huge improvement, and it will vary depending on hardware.

PostgreSQL query optimization

Postgres provides a very detailed guide to optimization which will definitely come in handy.

Clone this wiki locally