Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 9BF641337B4F for ; Fri, 25 Feb 2011 09:31:45 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 30424-03 for ; Fri, 25 Feb 2011 13:31:38 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from titanium.nettersworld.net (unknown [202.67.150.137]) by mail.postgresql.org (Postfix) with SMTP id 1E114133740B for ; Fri, 25 Feb 2011 09:31:36 -0400 (AST) Received: (qmail 84319 invoked from network); 25 Feb 2011 21:31:36 +0800 Received: from e139.ip.nettersworld.net (HELO localhost) (202.67.150.139) by titanium.nettersworld.net with SMTP; 25 Feb 2011 21:31:36 +0800 Received: from titanium.nettersworld.net ([202.67.150.137]) by localhost (titanium.nettersworld.net [127.0.0.1]) with ESMTP id 49553-08 for ; Fri, 25 Feb 2011 21:31:33 +0800 (HKT) Received: from longwl7 (unknown [58.247.211.11]) by titanium.nettersworld.net (Postfix) with ESMTP id 285D528453 for ; Fri, 25 Feb 2011 21:31:31 +0800 (HKT) From: "Sam Wong" To: Subject: Index use difference betweer LIKE, LIKE ANY? Date: Fri, 25 Feb 2011 21:31:26 +0800 Message-ID: <01e801cbd4f0$5153d840$f3fb88c0$@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: AcvU78gjV57Do25lRx+dg0gjjkaO/A== Content-Language: zh-hk X-Virus-Scanned: amavisd-new at nettersworld.net X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.107 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793 X-Spam-Level: X-Archive-Number: 201102/495 X-Sequence-Number: 42633 I found that "LIKE", "= ANY (...)", "LIKE .. OR LIKE .." against a text field used the index correctly, but not "LIKE ANY (...)". Would that be a bug? ---- Here is my table and index: CREATE TABLE shipment_lookup ( shipment_id text NOT NULL, lookup text NOT NULL ); CREATE INDEX shipment_lookup_prefix ONshipment_lookup USING btree (upper(lookup)); ---- The table have 10 million rows. The following statements use the index as expected: select * from shipment_lookup where (UPPER(lookup) = 'SD1102228482' or UPPER(lookup) ='ABCDEFGHIJK') select * from shipment_lookup where (UPPER(lookup) = ANY (ARRAY['SD1102228482','ABCDEFGHIJK'])) select * from shipment_lookup where (UPPER(lookup) LIKE 'SD1102228482%' or UPPER(lookup) LIKE 'ABCDEFGHIJK%') The following statement results in a full table scan (but this is what I really want to do): select * from shipment_lookup where (UPPER(lookup) LIKE ANY(ARRAY['SD1102228482%', 'ABCDEFGHIJK%'])) I could rewrite the LIKE ANY(ARRAY[...]) as an LIKE .. OR .. LIKE .., but I wonder what makes the difference? Thanks, Sam ---- Version Info: Postgresql: "PostgreSQL 8.4.5, compiled by Visual C++ build 1400, 32-bit" on Windows 2003