Re: 9.2.4: Strange behavior when wildcard is on left side of search string - Mailing list pgsql-bugs

From Christopher Browne
Subject Re: 9.2.4: Strange behavior when wildcard is on left side of search string
Date
Msg-id CAFNqd5UpXMt1geq2fb073EQCccwNp+zeuk3FqCfpw8K1omcKuw@mail.gmail.com
Whole thread Raw
In response to 9.2.4: Strange behavior when wildcard is on left side of search string  (ERR ORR <rd0002@gmail.com>)
List 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.

pgsql-bugs by date:

Previous
From: Kevin Grittner
Date:
Subject: Re: Re: 9.2.4: Strange behavior when wildcard is on left side of search string
Next
From: Tom Lane
Date:
Subject: Re: Re: 9.2.4: Strange behavior when wildcard is on left side of search string