Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 61F262E0056 for ; Fri, 28 Mar 2008 11:54:25 -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 73840-02 for ; Fri, 28 Mar 2008 11:54:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from europa.idg.com.au (europa.idg.com.au [203.34.46.9]) by postgresql.org (Postfix) with ESMTP id D9FEA2E0053 for ; Fri, 28 Mar 2008 11:54:16 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by europa.idg.com.au (Postfix) with ESMTP id 31CC133D1E; Sat, 29 Mar 2008 01:54:11 +1100 (EST) X-Virus-Scanned: Debian amavisd-new at metis.idg.com.au Received: from europa.idg.com.au ([127.0.0.1]) by localhost (metis.idg.com.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JwcIksLe4jXd; Sat, 29 Mar 2008 01:53:56 +1100 (EST) Received: by europa.idg.com.au (Postfix, from userid 1008) id AD74733D2A; Sat, 29 Mar 2008 01:53:47 +1100 (EST) Date: Fri, 28 Mar 2008 15:53:47 +0100 From: Gavin Sherry To: Dave Cramer Cc: postgresql performance list Subject: Re: using like in a prepare doesnt' use the right index Message-ID: <20080328145347.GE6360@europa.idg.com.au> References: <66BB263A-A7B3-4AA1-A350-96A3C44FA395@fastcrypt.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <66BB263A-A7B3-4AA1-A350-96A3C44FA395@fastcrypt.com> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/515 X-Sequence-Number: 29469 On Thu, Mar 27, 2008 at 03:14:49PM -0400, Dave Cramer wrote: > I have a query which is > > prepare s_18 as select uid from user_profile where name like > $1::varchar and isactive=$2 order by name asc limit 250; > > explain analyze execute s_18 ('atxchery%','t'); > QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------------------------------- > Limit (cost=0.00..7965.22 rows=250 width=14) (actual > time=301.714..3732.269 rows=1 loops=1) > -> Index Scan using user_profile_name_key on user_profile > (cost=0.00..404856.37 rows=12707 width=14) (actual > time=301.708..3732.259 rows=1 loops=1) > Filter: (((name)::text ~~ $1) AND (isactive = $2)) > Total runtime: 3732.326 ms > > without prepared statements we get > > explain analyze select uid from user_profile where name like 'foo%' > and isactive='t' order by name asc limit 250; > > QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------------------- > Limit (cost=293.89..294.08 rows=73 width=14) (actual > time=5.947..6.902 rows=250 loops=1) > -> Sort (cost=293.89..294.08 rows=73 width=14) (actual > time=5.943..6.258 rows=250 loops=1) > Sort Key: name > Sort Method: top-N heapsort Memory: 38kB > -> Bitmap Heap Scan on user_profile (cost=5.36..291.64 > rows=73 width=14) (actual time=0.394..2.481 rows=627 loops=1) > Filter: (isactive AND ((name)::text ~~ 'foo%'::text)) > -> Bitmap Index Scan on user_profile_name_idx > (cost=0.00..5.34 rows=73 width=0) (actual time=0.307..0.307 rows=628 > loops=1) > Index Cond: (((name)::text ~>=~ 'foo'::text) AND > ((name)::text ~<~ 'fop'::text)) > > > There are two indexes on it > > "user_profile_name_idx" UNIQUE, btree (name varchar_pattern_ops) > "user_profile_name_key" UNIQUE, btree (name) > > one for equality, one for like This is behaving as designed because the planner transforms the predicate in the second query: Index Cond: (((name)::text ~>=~ 'foo'::text) AND ((name)::text ~<~ 'fop'::text)). It cannot make this transformation for a prepared statement where the LIKE argument is a PREPARE parameter (the first query), since the transformation depends on inspecting the actual string. You could probably continue using prepared statements and make this transformation yourself but you'll have to be careful about creating the 'greater' string (see make_greater_string()). Come to think of it, it'd easier to just make a set returning function which executes this query, if you need to stick with prepare/execute. Thanks, Gavin