Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 042AD9FB368 for ; Wed, 18 Jul 2007 05:42:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 51813-10 for ; Wed, 18 Jul 2007 05:42:29 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.187]) by postgresql.org (Postfix) with ESMTP id CC8E49FA2B7 for ; Wed, 18 Jul 2007 05:42:27 -0300 (ADT) Received: by nf-out-0910.google.com with SMTP id f5so8469nfh for ; Wed, 18 Jul 2007 01:42:26 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=gRHSsjk2hvtaxKsaZQOQaiIPGof/5p1sU3PvqDkC6VOyWb5HDmKwv3820yyNJu+nVb6u7sMRQGGTc8pHFlTYbgUg6NUqcyBGQ0dHO/5yWoOyXx4+U/zHm+ll4t8tlTeTzY6UNkwn6YWP4Kd34A4h11qfqiZxxzxpo5WK9B3ocw0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=b/Bmavqk/QkP32PHNhtZEcO//FXGOqEEtbifPHsRJq/8zkUaJo0U1jCj74FFP2z3PbYturT/wv954vRe4H5vmupVK0yRg1dyDcBOU+Ra8wk6ZLrTEUKrhHN5W2gBv5nKQ09xFEbyKYLp19gWf/g1wGGdJKs2NfpzZi68Dl1EXuA= Received: by 10.82.100.1 with SMTP id x1mr1614156bub.1184748146011; Wed, 18 Jul 2007 01:42:26 -0700 (PDT) Received: from ?192.168.1.33? ( [82.20.57.141]) by mx.google.com with ESMTPS id f3sm624473nfh.2007.07.18.01.42.21 (version=SSLv3 cipher=RC4-MD5); Wed, 18 Jul 2007 01:42:23 -0700 (PDT) Message-ID: <469DD236.3060108@enterprisedb.com> Date: Wed, 18 Jul 2007 09:41:26 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: Tonio Caputo CC: pgsql-jdbc@postgresql.org Subject: Re: Index scan with like expressions References: <1184707776.7307.65.camel@heracles.melo> In-Reply-To: <1184707776.7307.65.camel@heracles.melo> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/79 X-Sequence-Number: 17253 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