Re: LIKE, leading percent, bind parameters and indexes

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: LIKE, leading percent, bind parameters and indexes
Дата
Msg-id 877j49qu7r.fsf@stark.xeocode.com
обсуждение исходный текст
Ответ на Re: LIKE, leading percent, bind parameters and indexes  ("Rodrigo Hjort" <rodrigo.hjort@gmail.com>)
Список pgsql-hackers
"Rodrigo Hjort" <rodrigo.hjort@gmail.com> writes:

> > I think more exactly, the planner can't possibly know how to plan an
> > indexscan with a leading '%', because it has nowhere to start.
> 
> The fact is that index scan is performed on LIKE expression on a string not
> preceded by '%', except when bound parameter is used.
> 
> select * from table where field like 'THE NAME%'; -- index scan
> select * from table where field like '%THE NAME%'; -- seq scan
> select * from table where field like :bind_param; -- seq scan (always)

Just for reference I found that both Oracle and MSSQL (back when last I used
it, many years ago) did use an index scan for the following case:

select * from table where field like :bind_param || '%'

At the time this seemed perfectly logical but now that I have more experience
it seems hard to justify. There's no principled reason to think this is any
more likely than a plain :bind_param to be an indexable scan. 

However in practice this worked great. I rarely if ever put % characters into
the bind parameter and the index scan was exactly what I, as a user, expected.

Even if there's resistance to having this form be treated as indexable there
is certainly a use case for something like this. If not this then something
like

WHERE escape(:bind_param)||'%'

but that would be pretty hard to recognize, certainly much harder than a
simple :bind_param || '%'.


-- 
greg



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Josh Berkus
Дата:
Сообщение: Re: Gborg and pgfoundry
Следующее
От: Hans-Jürgen Schönig
Дата:
Сообщение: Bug with UTF-8 character