Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOI0s-0005dK-EK for pgsql-performance@postgresql.org; Wed, 17 Oct 2012 01:01:34 +0000 Received: from [202.181.174.115] (helo=doravmmail.chowchi.com) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TOI0i-0000qj-OX for pgsql-performance@postgresql.org; Wed, 17 Oct 2012 01:01:29 +0000 Received: (qmail 16587 invoked from network); 17 Oct 2012 09:01:20 +0800 Received: from unknown (HELO doravmmail.chowchi.com) (202.181.174.115) by doravmmail.chowchi.com with SMTP; 17 Oct 2012 09:01:20 +0800 X-Virus-Scanned: amavisd-new at chowchi.com Received: from doravmmail.chowchi.com ([202.181.174.115]) by doravmmail.chowchi.com (doravmmail.chowchi.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id renleaUUOpFs for ; Wed, 17 Oct 2012 09:01:15 +0800 (HKT) Received: from longwt3 (123202019132.ctinets.com [123.202.19.132]) by doravmmail.chowchi.com (Postfix) with ESMTPA id ECBEE34773A for ; Wed, 17 Oct 2012 09:01:13 +0800 (HKT) From: "Sam Wong" Cc: References: <002501cdab76$62e38160$28aa8420$@hellosam.net> In-Reply-To: Subject: Re: LIKE op with B-Tree Index? Date: Wed, 17 Oct 2012 09:01:09 +0800 Message-ID: <002401cdac02$e5399280$afacb780$@hellosam.net> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQGusd8cGOrUTWmqx97JblJZXDKWdAK6kM8Wl+TLtoA= Content-Language: en-us X-Host-Lookup-Failed: Reverse DNS lookup failed for 202.181.174.115 (failed) X-Pg-Spam-Score: -0.1 (/) X-Archive-Number: 201210/224 X-Sequence-Number: 48183 > On Wednesday, October 17, 2012 4:30, Merlin Moncure wrote, > > On Tue, Oct 16, 2012 at 3:15 AM, Sam Wong wrote: > > Hi communities, > > > > I am investigating a performance issue involved with LIKE 'xxxx%' on > > an index in a complex query with joins. > > > > The problem boils down into this simple scenario---: > > ====Scenario==== > > My database locale is C, using UTF-8 encoding. I tested this on 9.1.6 and 9. > > 2.1. > > > > Q1. > > SELECT * FROM shipments WHERE shipment_id LIKE '12345678%' > > > > Q2. > > SELECT * FROM shipments WHERE shipment_id >= '12345678' AND > > shipment_id < '12345679' > > > > ...snip... > > > > ====Question==== > > Is Q1 and Q2 equivalent? From what I see and the result they seems to > > be the same, or did I miss something? (Charset: C, Encoding: UTF-8) If > > they are equivalent, is that a bug of the planner? > > They are most certainly not equivalent. What if the shipping_id is > 12345678Z? > > merlin > But '12345678Z' is indeed >= '12345678' AND < '12345679'. Just like 'apple' < 'apples' < 'apply' in a dictionary. A quick test: vitalink=# select * from ss; id ----------- 12345678 12345678Z 12345679 (3 rows) vitalink=# select * from ss WHERE id >= '12345678' AND id < '12345679'; id ----------- 12345678 12345678Z (2 rows) Sam