Re: Best way to use indexes for partial match at beginning

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Andrus <eetasoft(at)online(dot)ee>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Best way to use indexes for partial match at beginning
Date: 2005-11-09 21:08:10
Message-ID: 20051109210810.GF713@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Nov 09, 2005 at 10:46:27PM +0200, Andrus wrote:
> thank you. I try to formulate my problem more presicely.
> I have table
>
> CREATE TABLE foo ( bar CHAR(10) PRIMARY KEY);
>
> Cluster locale is non-C. Database encoding is UTF-8. Postgres vers is 8.1

Do this instead:

CREATE TABLE foo ( bar CHAR(10) NOT NULL );
CREATE UNIQUE INDEX foo_bar ON foo(bar char_pattern_ops);

> I want to run fast queries by knowing first characters of bar like :
>
> 1. Select records from foo where first character of bar is A
> 2. Select records from foo where first character of bar is B
> 3. Select records from foo where first two characters of bar are BC
> 4. Select records from foo where first three characters of bar are ABC

SELECT * FROM foo WHERE bar LIKE 'A%';
SELECT * FROM foo WHERE bar LIKE 'B%';
SELECT * FROM foo WHERE bar LIKE 'BC%';
SELECT * FROM foo WHERE bar LIKE 'ABC%';

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jaime Casanova 2005-11-09 21:15:32 Re: Best way to use indexes for partial match at beginning
Previous Message Scott Marlowe 2005-11-09 21:05:03 Re: Best way to use indexes for partial match at