Re: like & optimization

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Torsten Förtsch <torsten(dot)foertsch(at)gmx(dot)net>
Cc: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>, Postgresql General <pgsql-general(at)postgresql(dot)org>
Subject: Re: like & optimization
Date: 2013-10-14 21:40:39
Message-ID: CAHyXU0yMCbFCsL_d1rpfX95FEqQ7yvdYn5pv9ckT10FzwSfhMA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Oct 12, 2013 at 4:28 PM, Torsten Förtsch
<torsten(dot)foertsch(at)gmx(dot)net> wrote:
> 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.

pg_trgm module optimizes 'like with wildcards' without those
restrictions. It's very fast for what it does. Because of the
GIST/GIN dependency index only scans are not going to be used through
pg_tgrm though.

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Glen Eustace 2013-10-14 21:57:59 Re: mod_auth_pgsql and Apache 2.4
Previous Message Tomas Vondra 2013-10-14 20:37:40 Re: Postgresql 9.0.13 core dump