X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 8726D9DCD7B for ; Thu, 2 Mar 2006 21:28:42 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 24660-02 for ; Thu, 2 Mar 2006 21:28:45 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from linda-3.paradise.net.nz (bm-3a.paradise.net.nz [203.96.152.182]) by postgresql.org (Postfix) with ESMTP id E80B89DCD6F for ; Thu, 2 Mar 2006 21:28:39 -0400 (AST) Received: from smtp-3.paradise.net.nz (tclsnelb1-src-1.paradise.net.nz [203.96.152.172]) by linda-3.paradise.net.nz (Paradise.net.nz) with ESMTP id <0IVJ003PN2RUKK@linda-3.paradise.net.nz> for pgsql-performance@postgresql.org; Fri, 03 Mar 2006 14:28:42 +1300 (NZDT) Received: from [192.168.1.11] (218-101-29-68.dsl.clear.net.nz [218.101.29.68]) by smtp-3.paradise.net.nz (Postfix) with ESMTP id 2541831FE93; Fri, 03 Mar 2006 14:28:42 +1300 (NZDT) Date: Fri, 03 Mar 2006 14:28:40 +1300 From: Mark Kirkwood Subject: Re: Like 'name%' is not using index In-reply-to: To: Jozsef Szalay Cc: pgsql-performance@postgresql.org Message-id: <44079BC8.3030209@paradise.net.nz> MIME-version: 1.0 Content-type: text/plain; charset=windows-1252; format=flowed Content-transfer-encoding: 8BIT X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0.6 (X11/20051106) References: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.183 required=5 tests=[AWL=0.183] X-Spam-Score: 0.183 X-Spam-Level: X-Archive-Number: 200603/15 X-Sequence-Number: 17452 Jozsef Szalay wrote: > Hi all, > > > > I have to provide a pretty standard query that should return every row > where the NAME attribute begins with a specific string. The type of the > NAME column is varchar. I do have an index for this column. One would > think that Postgres will use the index to look up the matches, but > apparently that is not the case. It performs a full table scan. My > query looks something like this: > > > > SELECT * FROM table WHERE name LIKE ‘smith%’; > > > > Does anyone know a way to “force” the optimizer to utilize the index? Is > there perhaps another way of doing this? > Can you provide an EXPLAIN ANALYZE for the query? This will give us a hint as to why the index has not been chosen. The other standard gotcha is that LIKE will not use an index if your cluster is initialized with locale != C. If it is, then you can try recreating the index using something like: CREATE INDEX table_name ON table (name varchar_pattern_ops); cheers Mark