pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: PFC <lists@boutiquenumerique.com>
Cc: Layet Benjamin <benjamin@moonfactory.co.jp>
Cc: pgsql-performance@postgresql.org
Subject: Re: best practices with index on varchar column
Date: Tue, 22 Mar 2005 13:08:14 -0500
Message-ID: <10502.1111514894@sss.pgh.pa.us> (raw)
In-Reply-To: <op.sn1ccygeth1vuj@localhost>
References: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp>
	<op.sn1ccygeth1vuj@localhost>

PFC <lists@boutiquenumerique.com> 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



view thread (7+ messages)  latest in thread

Message-ID: <10502.1111514894@sss.pgh.pa.us>
Permalink:  ../10502.1111514894@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10502.1111514894@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, lists@boutiquenumerique.com, benjamin@moonfactory.co.jp
  Subject: Re: best practices with index on varchar column
  In-Reply-To: <10502.1111514894@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox