From: | "Edson Carlos Ericksson Richter" <richter(at)simkorp(dot)com(dot)br> |
---|---|
To: | <pgsql-general(at)postgresql(dot)org> |
Subject: | RES: Does postgresql 9.0.4 use index on PREFIX%SUFFIX queries? |
Date: | 2011-09-27 13:59:03 |
Message-ID: | 009601cc7d1d$9e581cc0$db085640$@com.br |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
> -----Mensagem original-----
> De: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-
> owner(at)postgresql(dot)org] Em nome de Marti Raudsepp
> Enviada em: segunda-feira, 26 de setembro de 2011 17:42
> Para: Edson Carlos Ericksson Richter
> Cc: pgsql-general(at)postgresql(dot)org
> Assunto: Re: [GENERAL] Does postgresql 9.0.4 use index on PREFIX%SUFFIX
> queries?
>
> On Mon, Sep 26, 2011 at 15:16, Edson Carlos Ericksson Richter
> <richter(at)simkorp(dot)com(dot)br> wrote:
> > select * from notafiscal where numeroctc like ‘POA%34345’;
> >
> > Prefix is normally 3 characters, suffix varyies.
> >
> > Is Postgresql 9.0.4 able to use an BTREE index on notafiscal.numeroctc
> to execute this query?
>
> As mentioned by other posters, you should use a btree index with
> text_pattern_ops opclass to speed up this query.
>
> For queries like these, it's often faster to match the text in *reverse*.
> You can create two indexes like this:
>
> create index on foobar (txt text_pattern_ops); create index on foobar
> (reverse(txt) text_pattern_ops);
>
> And then write your queries like this:
> SELECT * FROM foobar
> WHERE txt like 'POA%34345'
> AND reverse(txt) like reverse('POA%34345');
Just perfect! It not only works, but time dropped from 5s to 94ms.
Regards,
Edson.
>
> PostgreSQL will automatically choose one or both indexes for executing
> this query.
>
> Regards,
> Marti
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org) To make
> changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
From | Date | Subject | |
---|---|---|---|
Next Message | pasman pasmański | 2011-09-27 14:39:50 | Re: New feature: accumulative functions. |
Previous Message | Edson Carlos Ericksson Richter | 2011-09-27 13:39:36 | RES: Does postgresql 9.0.4 use index on PREFIX%SUFFIX queries? |