Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6877163235D for ; Thu, 18 Feb 2010 08:18:26 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 88519-06 for ; Thu, 18 Feb 2010 12:18:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mail.postgresql.org (Postfix) with ESMTP id 902216321F8 for ; Thu, 18 Feb 2010 08:18:13 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mreu2) with ESMTP (Nemesis) id 0MeOaJ-1NJ86H2C59-00Pjwk; Thu, 18 Feb 2010 13:18:11 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1Ni5Kc-0005Cd-00 for pgsql-performance@postgresql.org; Thu, 18 Feb 2010 13:18:10 +0100 Date: Thu, 18 Feb 2010 13:18:10 +0100 From: "A. Kretschmer" To: pgsql-performance@postgresql.org Subject: Re: index usage in not like Message-ID: <20100218121810.GG23676@a-kretschmer.de> References: <2a7905441002180355w6dc89a87y8643c0a7f881072a@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX18uM0iloo+FqX1uckPg0MXywnCwHcYQE0HdoZU ah35ELX7JiMCG1Upczv6Z8l4U7U9PeRQ2gW2NgvLXXzuPc/AP1 bZomLfXxyYLKcvHoHZX9Q== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.763 tagged_above=-10 required=5 tests=AWL=-0.764, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201002/273 X-Sequence-Number: 37694 In response to Thom Brown : > On 18 February 2010 11:55, AI Rumman wrote: > > "Not like" operation does not use index. > > > > select * from vtiger_contactscf where lower(cf_1253) not like > > lower('Former%') > > > > I created index on lower(cf_1253). > > > > How can I ensure index usage in not like operation? > > Anyone please help. > > > > How many rows do you have in your table? If there are relatively few, > it probably guesses it to be cheaper to do a sequential scan and > calculate lower values on-the-fly rather than bother with the index. That's one reason, an other reason, i think, is, that a btree-index can't search with an 'not like' - operator. test=*# insert into words select 'fucking example' from generate_series(1,10000); INSERT 0 10000 test=*# insert into words select 'abc' from generate_series(1,10); INSERT 0 10 test=*# explain select * from words where lower(w) like lower('a%') or lower(w) like lower('b%'); QUERY PLAN ------------------------------------------------------------------------------------- Bitmap Heap Scan on words (cost=1538.75..6933.39 rows=55643 width=36) Recheck Cond: ((lower(w) ~~ 'a%'::text) OR (lower(w) ~~ 'b%'::text)) Filter: ((lower(w) ~~ 'a%'::text) OR (lower(w) ~~ 'b%'::text)) -> BitmapOr (cost=1538.75..1538.75 rows=57432 width=0) -> Bitmap Index Scan on idx_words (cost=0.00..1027.04 rows=39073 width=0) Index Cond: ((lower(w) ~>=~ 'a'::text) AND (lower(w) ~<~ 'b'::text)) -> Bitmap Index Scan on idx_words (cost=0.00..483.90 rows=18359 width=0) Index Cond: ((lower(w) ~>=~ 'b'::text) AND (lower(w) ~<~ 'c'::text)) (8 rows) test=*# explain select * from words where lower(w) not like lower('a%') or lower(w) like lower('b%'); QUERY PLAN ------------------------------------------------------------------- Seq Scan on words (cost=0.00..10624.48 rows=282609 width=36) Filter: ((lower(w) !~~ 'a%'::text) OR (lower(w) ~~ 'b%'::text)) (2 rows) In other words: revert your where-condition from 'not like' to multiple 'like' conditions for all letters except 'f%'. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99