Re: 9.2.4: Strange behavior when wildcard is on left side of search string

From: Christopher Browne <cbbrowne(at)gmail(dot)com>
To: ERR ORR <rd0002(at)gmail(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: 9.2.4: Strange behavior when wildcard is on left side of search string
Date: 2013-04-07 15:45:49
Message-ID: CAFNqd5UpXMt1geq2fb073EQCccwNp+zeuk3FqCfpw8K1omcKuw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

This doesn't seem either buggy or strange...

An index on the ordering of that column is not helpful in handling a
leading wildcard, and so the query optimizer will, in such cases, revert,
correctly, to using a sequential scan and filtering the results.

If you have cases where this sort of wildcard needs to be fast, a
functional index could help.

Create index foo on tbl (reverse(col));

And reverse the wildcard so the index is usable:

Select * from tbl where reverse(col) like 'esrever%';

That query can harness the reversed index.

Unfortunately, no ordered index helps with

Select * from too where col like '%something%';

For that, a full text search index can help, but that is a longer story.

At any rate, what you are observing is no surprise, and consistent with
what many database systems do.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2013-04-07 15:48:19 Re: Re: 9.2.4: Strange behavior when wildcard is on left side of search string
Previous Message Kevin Grittner 2013-04-07 15:22:46 Re: Re: 9.2.4: Strange behavior when wildcard is on left side of search string