title: PreparedStatement very slow, but manual query quick
body: <p>I have a preparedstatement from a framework as followed:</p>
  
  <pre><code>SELECT OH.ORDER_ID, MAX(OS.STATUS_DATETIME) FROM public.ORDER_HEADER OH, public.ORDER_STATUS OS WHERE ((OH.ORDER_ID = OS.ORDER_ID AND OH.STATUS_ID = ? AND OS.STATUS_ID = ?)) GROUP BY OH.ORDER_
  ID HAVING (MAX(OS.STATUS_DATETIME) &lt;= ?) ORDER BY OH.ORDER_ID ASC
  </code></pre>
  
  <p>OrderHeader has 300 000 rows, OrderStatus has roughly 5-6 million.
  All queried fields have an index on them. The database is Postgres 9.1</p>
  
  <pre><code>SELECT OH.ORDER_ID, MAX(OS.STATUS_DATETIME) FROM ORDER_HEADER OH INNER JOIN ORDER_STATUS OS 
      ON ((OH.ORDER_ID = OS.ORDER_ID AND OH.STATUS_ID = 'ORDER_PARTIALLY_RECEIVED' AND OS.STATUS_ID = 'ORDER_PARTIALLY_RECEIVED'))
       GROUP BY OH.ORDER_ID HAVING (MAX(OS.STATUS_DATETIME) &lt;= '2015-01-27 00:00:00') ORDER BY OH.ORDER_ID ASC
  </code></pre>
  
  <p>This is the query string when the parameters are substitued in.</p>
  
  <p>Running the query from Java, as a PreparedStatement and parameters set in there is making the query to run for minutes (5-6 minutes).
  When i run the SQL manually, it takes 10 seconds.
  How is this possible, how can i explain it?</p>