Re: optimizing SELECT with LIKE

From: Ericson Smith <eric(at)did-it(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: jsarmiento(at)ccom(dot)org
Subject: Re: optimizing SELECT with LIKE
Date: 2002-06-12 17:25:16
Message-ID: 1023902717.2047.16.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hmmm....

That's gonna be super slow.

You might want to create 2 additional tables:

keywords
--------
id
keyword

searchmap
---------
keywordid
documentid

Then index your data so that
1. There is a unique entry in keywords for your existing data
2. Your searchmap would have an entry for keywordid, and a documentid

To search for data...
1. Find the ID of the keywords in question eg:
JORGE => 55
SARMIENTO => 89

2. Get data from searchmap like so:
SELECT documentid, count(*) as hits FROM searchmap WHERE keywordid IN
(55,89) GROUP BY documentid ORDER BY hits DESC

3. Get your documentid's from the document id in #2 above.

This will *ligntning* fast.

- Ericson Smith
eric(at)did-it(dot)com
http://www.did-it.com

On Wed, 2002-06-12 at 11:43, Jorge Sarmiento wrote:
> I have 3000000 rows in a database where I have to make a:
>
> SELECT name FROM table WHERE name LIKE '%firstname%' AND name LIKE
> '%secondname%';
>
> to obtain the data I need.
>
> Due to the data nature, there is no other way to look for the data, it's old
> data that was registered in paper years ago, with no searchable index... just
> names, that sometimes are wrote like: JORGE SARMIENTO, other times like:
> SARMIENTO, JORGE and other times like JORGE LUIS SARMIENTO. and due to legal
> reasons, the data must be entered the exact way it was wroten.
>
> Is there any way to optimize postgres for doing this kind of SELECT?
>
> thanx in advance!
>
> Jorge S.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2002-06-12 17:29:33 Re: [BUGS] createdb comments
Previous Message Tom Burke 2002-06-12 17:21:24 Re: Updates are slow..