pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <heikki@enterprisedb.com>
To: Tonio Caputo <tonioc@exeo.com.ar>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Index scan with like expressions
Date: Wed, 18 Jul 2007 09:41:26 +0100
Message-ID: <469DD236.3060108@enterprisedb.com> (raw)
In-Reply-To: <1184707776.7307.65.camel@heracles.melo>
References: <1184707776.7307.65.camel@heracles.melo>

Tonio Caputo wrote:
> I'm having some trouble with index scan using like expressions, using
> JDBC, but not in psql where the index is used.
> ...
> My query:
>     select beings_id from inscriptions
>     where code like '999999';
> ...
> If I do it from my java application explicitly writing my code value
> in the sql-string I get the correct plan.

Index can only be used for a LIKE expression if there's no % or _ in the
beginning of the string. For example, "LIKE 'foobar'" and "LIKE 'foo%'"
can use the index, looking for the string 'foobar', or anything that
begins with 'foo'. But for expressions like "LIKE '%bar'", the index
can't be used.

If you use a parameter marker, "LIKE ?", the planner doesn't know if the
string you're going to give as parameter is such that the index can be
used, so it has no choice but choose a plan that works regardless of the
parameter value, which is a seq scan in this case.

You can use prepareThreshold=0 connection parameter to disable
server-side prepared statements, so that the query is planned every time
it's executed. That way the planner can check the parameter value each
time, and use the index when possible.

-- 
  Heikki Linnakangas
  EnterpriseDB   http://www.enterprisedb.com



view thread (3+ messages)  latest in thread

Message-ID: <469DD236.3060108@enterprisedb.com>
Permalink:  ../469DD236.3060108@enterprisedb.com/
Also on:    postgresql.org/message-id/469DD236.3060108@enterprisedb.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: heikki@enterprisedb.com, tonioc@exeo.com.ar
  Subject: Re: Index scan with like expressions
  In-Reply-To: <469DD236.3060108@enterprisedb.com>
  References: <1184707776.7307.65.camel@heracles.melo> <469DD236.3060108@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox