Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id F3A20650E96 for ; Sat, 28 Jun 2008 16:59:19 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 95444-07 for ; Sat, 28 Jun 2008 16:59:14 -0300 (ADT) X-Greylist: delayed 00:39:40.067237 by SQLgrey-1.7.6 Received: from cassarossa.samfundet.no (cassarossa.samfundet.no [129.241.93.19]) by postgresql.org (Postfix) with ESMTP id B46AA650E79 for ; Sat, 28 Jun 2008 16:59:16 -0300 (ADT) Received: from trofast.ipv6.sesse.net ([2001:700:300:1803:20e:cff:fe36:a766] helo=trofast.sesse.net) by cassarossa.samfundet.no with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1KCfxM-0008Uu-Jr; Sat, 28 Jun 2008 21:19:32 +0200 Received: from root by trofast.sesse.net with local (Exim 4.69) (envelope-from ) id 1KCfxL-0003Pt-UR; Sat, 28 Jun 2008 21:19:31 +0200 Date: Sat, 28 Jun 2008 21:19:31 +0200 From: "Steinar H. Gunderson" To: Moritz Onken Cc: pgsql-performance@postgresql.org Subject: Re: Planner should use index on a LIKE 'foo%' query Message-ID: <20080628191931.GB13013@uio.no> Mail-Followup-To: Moritz Onken , pgsql-performance@postgresql.org References: <287E4428-BECC-46D6-BC47-EA94EA0121F0@houseofdesign.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <287E4428-BECC-46D6-BC47-EA94EA0121F0@houseofdesign.de> X-Operating-System: Linux 2.6.20.4 on a x86_64 User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/206 X-Sequence-Number: 30556 On Sat, Jun 28, 2008 at 06:24:42PM +0200, Moritz Onken wrote: > SELECT distinct url from item where url like 'http://www.micro%' limit > 10; Here, the planner knows the pattern beforehand, and can see that it's a simple prefix. > select * > from result > where exists > (select * from item where item.url LIKE result.url || '%' limit 1) > limit 10; Here it cannot (what if result.url was '%foo%'?). Try using something like (item.url >= result.url && item.url <= result.url || 'z'), substituting an appropriately high character for 'z'. > The only explaination is that I don't use a constant when comparing the > values. But actually it is a constant... It's not a constant at planning time. Also note that you'd usually want to use IN instead of a WHERE EXISTS. /* Steinar */ -- Homepage: http://www.sesse.net/