X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 90CFA9FA111 for ; Tue, 6 Jun 2006 06:58:06 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 51272-02 for ; Tue, 6 Jun 2006 06:57:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id C9F9B9FA315 for ; Tue, 6 Jun 2006 06:57:57 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 86E9131946; Tue, 6 Jun 2006 11:57:56 +0200 (MET DST) From: "Andrus" X-Newsgroups: pgsql.performance Subject: Re: How to force Postgres to use index on ILIKE Date: Tue, 6 Jun 2006 12:57:31 +0300 Organization: Hub.Org Networking Services Lines: 39 Message-ID: References: <200606051426.56789.josh@agliodbs.com> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 X-RFC2646: Format=Flowed; Original To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/50 X-Sequence-Number: 19407 >> SELECT toode, nimetus >> FROM toode >> WHERE toode ILIKE 'x10%' ESCAPE '!' >> ORDER BY UPPER(toode ),nimetus LIMIT 100 >> >> runs 1 minute in first time for small table size. >> >> Toode field type is CHAR(20) > > 1) why are you using CHAR and not VARCHAR or TEXT? CHAR will give you > problems using an index, period. 1. I haven't seen any example where VARCHAR is better that CHAR for indexing 2. I have a lot of existing code. Changing CHAR to VARCHAR requires probably re-writing a lot of code, a huge work. > 2) You can't use an index on ILIKE. I'ts very sad. I expected that lower(toode) index can be used. > You can, however, use an index on > lower(field) if your query is properly phrased and if you've created an > expression index on lower(field). I tried by Postgres does not use index. Why ? create index nimib2 on firma1.klient(lower(nimi) bpchar_pattern_ops); explain analyze select nimi from firma1.klient where lower(nimi) like 'mokter%' "Seq Scan on klient (cost=0.00..9.79 rows=1 width=74) (actual time=0.740..0.761 rows=1 loops=1)" " Filter: (lower((nimi)::text) ~~ 'mokter%'::text)" "Total runtime: 0.877 ms"