Index not used with prepared statement

From: Guido Neitzer <guido(dot)neitzer(at)pharmaline(dot)de>
To: pgsql-performance(at)postgresql(dot)org
Subject: Index not used with prepared statement
Date: 2005-09-11 08:29:09
Message-ID: 33A6D7B7-FDF3-456E-9154-402E9C5BC5D9@pharmaline.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi.

I have a performance problem with prepared statements (JDBC prepared
statement).

This query:

PreparedStatement st = conn.prepareStatement("SELECT id FROM
dga_dienstleister WHERE plz like '45257'");

does use an index.

This query:

String plz = "45257";
PreparedStatement st = conn.prepareStatement("SELECT id FROM
dga_dienstleister WHERE plz like ?");
st.setString(1, plz);

does NOT use an index.

As it should in the end result in absolutely the same statement, the
index should be used all the time. I have to set the
protocolVersion=2 and use the JDBC2 driver to get it working (but
then the statements are created like in the first query, so no
solution, only a workaround).

I'm not sure whether this is a bug (I think it is) or a problem of
understanding.

Known problem?

I have tried PG 8.0.1, 8.0.3, 8.1beta with the JDBC-drivers

- postgresql-8.0-312.jdbc2.jar --> okay with protocolVersion=2 in the
URL
- postgresql-8.0-312.jdbc3.jar --> not okay whatever I do

I'm on Mac OS X, if that matters.

cug

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Andreas Seltenreich 2005-09-11 09:03:23 Re: Index not used with prepared statement
Previous Message Gavin Sherry 2005-09-11 08:05:03 Re: performance discrepancy indexing one column versus