Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 4D8F39FBE9F for ; Wed, 18 Jul 2007 14:30:21 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 20018-06 for ; Wed, 18 Jul 2007 14:30:15 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from fe1.millic.com.ar (mx059.millic.com.ar [200.81.249.59]) by postgresql.org (Postfix) with ESMTP id BE8919FBEB1 for ; Wed, 18 Jul 2007 14:30:16 -0300 (ADT) Received: from 200-122-4-253.dsl.prima.net.ar ([200.122.4.253]:8674 "EHLO localhost.localdomain" smtp-auth: "tonioc@exeo.com.ar") by mr1.millic.com.ar with ESMTP id S1746274AbXGRR3t (ORCPT ); Wed, 18 Jul 2007 14:29:49 -0300 X-Comment: RFC 2476 MSA function at fe1.millic.com.ar logged sender identity as: tonioc@exeo.com.ar Received: from [192.168.1.51] (helo=[192.168.1.51]) by localhost.localdomain with esmtp (Exim 4.50) id 1IBDCT-0006ho-LK for pgsql-jdbc@postgresql.org; Wed, 18 Jul 2007 14:20:33 -0300 Subject: Re: Index scan with like expressions From: Tonio Caputo To: pgsql-jdbc@postgresql.org In-Reply-To: <469DD236.3060108@enterprisedb.com> References: <1184707776.7307.65.camel@heracles.melo> <469DD236.3060108@enterprisedb.com> Content-Type: text/plain Date: Wed, 18 Jul 2007 14:19:31 -0300 Message-Id: <1184779171.7307.69.camel@heracles.melo> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 Content-Transfer-Encoding: 7bit X-Millicom-Alerce-Info: Al X-Millicom-Alerce-MRId: e43e03b80d2bc7ee5bb9d00ae4c2b0f5 X-Millicom-Alerce-From: tonioc@exeo.com.ar X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.251 tagged_above=0 required=5 tests=AWL=0.250, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200707/82 X-Sequence-Number: 17256 Thanks heikki, for your quick and very precise answer. I understood the problem soon after sending the e-mail, but I didn't know nothing about the prepareThreshold=0 parameter. thanks again tonio On Wed, 2007-07-18 at 09:41 +0100, Heikki Linnakangas wrote: > 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. >