Re: Sorting nulls and empty strings together

Поиск
Список
Период
Сортировка
От Gregory Stark
Тема Re: Sorting nulls and empty strings together
Дата
Msg-id 87prs84w2d.fsf@oxford.xeocode.com
обсуждение исходный текст
Ответ на Sorting nulls and empty strings together  ("Andrus" <kobruleht2@hot.ee>)
Список pgsql-general
"Andrus" <kobruleht2@hot.ee> writes:

> User interface need to show nulls as empty strings.
> PostgreSQL sorts nulls after all data.
>...
> Select statements are generated dynamically by driver and it is not easy
> to change them to generate order by coalesce( testcol,'').

You could use NULLS FIRST (assuming your collation has '' sorted at the
beginning which I think is normally true). But you would have to switch it to
NULLS LAST if you sort descending...

> If there is no other way I can change driver to generate coalesce(
> testcol,'') as order by expressions.
>
> However I'm afraid that those order by expression cannot use regular index
> like
>
> create index test_inx on test(testcol)

create index test_inx on test(coalesce(testcol,''))

But I bet you'll have trouble using an index at all for the order by. You'll
either be searching on other columns which would have to be leading columns of
every index or you'll be reading the whole table anyways and postgres will
prefer to sort since it's faster.

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com
  Ask me about EnterpriseDB's 24x7 Postgres support!

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

Предыдущее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: Sorting nulls and empty strings together
Следующее
От: Stephan Szabo
Дата:
Сообщение: Re: String Comparison and NULL