X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CDD6856A45 for ; Tue, 22 Mar 2005 18:08:27 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 18144-07 for ; Tue, 22 Mar 2005 18:08:19 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 011C156A0D for ; Tue, 22 Mar 2005 18:08:18 +0000 (GMT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j2MI8EDd010503; Tue, 22 Mar 2005 13:08:14 -0500 (EST) To: PFC Cc: "Layet Benjamin" , pgsql-performance@postgresql.org Subject: Re: best practices with index on varchar column In-reply-to: References: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> Comments: In-reply-to PFC message dated "Tue, 22 Mar 2005 11:49:36 +0100" Date: Tue, 22 Mar 2005 13:08:14 -0500 Message-ID: <10502.1111514894@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/458 X-Sequence-Number: 11163 PFC writes: >> Can I use an index on a varchar column to optimize the SELECT queries >> that use " column LIKE 'header%' "? > Yes > Note that if you want case insensitive matching you need to make an index > on lower(column) and SELECT WHERE lower(column) LIKE 'header%' > Locales may bite you. Yes. If your database locale is not "C" then the default btree index behavior does not match up with what LIKE needs. In that case you need a special index using the appropriate "pattern_ops" opclass, eg CREATE INDEX test_index ON test_table (col varchar_pattern_ops); or if you want case insensitive matching CREATE INDEX test_index ON test_table (lower(col) varchar_pattern_ops); and then write the queries with lower() as PFC illustrates. *Don't* use ILIKE --- it basically can't use indexes at all. For more info see http://www.postgresql.org/docs/8.0/static/indexes-opclass.html regards, tom lane