Re: like & optimization

From: Torsten Förtsch <torsten(dot)foertsch(at)gmx(dot)net>
To: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>, Postgresql General <pgsql-general(at)postgresql(dot)org>
Subject: Re: like & optimization
Date: 2013-10-12 21:28:06
Message-ID: 5259BEE6.2060500@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2013-10-12 21:40:36 Re: Forms for entering data into postgresql
Previous Message Adrian Klaver 2013-10-12 20:57:55 Re: Forms for entering data into postgresql