Re: Query optimiser is not using 'not null' constraint when 'order by nulls last' clause is used

From: Nandakumar M <m(dot)nanda92(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Query optimiser is not using 'not null' constraint when 'order by nulls last' clause is used
Date: 2018-02-02 14:04:30
Message-ID: CANcFUu4YyAFYUYw2P=UGTJNo4TKFqSMOjnx5pi1apxiuTqeR+g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi,

On 2 Feb 2018 15:06, "Laurenz Albe" <laurenz(dot)albe(at)cybertec(dot)at> wrote:

>In the above case, the optimizer does >not know that it will get the rows
>in the correct order: indexes are >sorted ASC NULLS LAST by default,
>so a backwards index scan will >produce the results NULLS FIRST,
>which is the default for ORDER BY ... >DESC.

The order by column has a not null constraint on it and so nulls last or
first shouldn't make any difference.

>If you want the nulls last, PostgreSQL >has to retrieve *all* the rows and
sort
>them rather than using the first 25 >results it gets by scanning then
>indexes.

>To have the above query perform >fast, add additional indexes with either
>ASC NULLS FIRST or DESC NULLS >LAST for all used keys.

For now this is exactly what I have done. But it is in effect a duplicate
index on a PK column and I would be happy not to create it in the first
place.

Regards
Nanda

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2018-02-02 15:00:16 Re: Query optimiser is not using 'not null' constraint when 'order by nulls last' clause is used
Previous Message Vitaliy Garnashevich 2018-02-02 11:46:22 Re: effective_io_concurrency on EBS/gp2