Re: like & optimization - Mailing list pgsql-general

From Torsten Förtsch
Subject Re: like & optimization
Date
Msg-id 5259BEE6.2060500@gmx.net
Whole thread Raw
In response to like & optimization  (Scott Ribe <scott_ribe@elevated-dev.com>)
Responses Re: like & optimization
List pgsql-general
On 12/10/13 20:08, Scott Ribe wrote:
> select * from test where tz >= start and tz < end and colb like '%foobar%'

I think you can use an index only for wildcard expressions that are
anchored at the beginning. So,

  select * from test where tz >= start and tz < end
     and colb like 'foobar%'

can use an index on colb.

You could perhaps

  select * from test where tz >= start and tz < end
     and colb like 'foobar%'
  union all
  select * from test where tz >= start and tz < end
     and reverse(colb) like 'raboof%'

Then you need 2 indexes, one on colb the other on reverse(colb).

You can have duplicates in the result set if the table contains rows
where colb='foobar'. If that's a problem, use union distinct.

Alternatively, if foobar is kind of a word (with boundaries), you could
consider full-text search.

Just my 2¢,
Torsten


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Forms for entering data into postgresql
Next
From: David Johnston
Date:
Subject: Re: Forms for entering data into postgresql