Re: Query optimiser is not using 'not null' constraint when 'order bynulls last' clause is used - Mailing list pgsql-performance

From Nandakumar M
Subject Re: Query optimiser is not using 'not null' constraint when 'order bynulls last' clause is used
Date
Msg-id CANcFUu4YyAFYUYw2P=UGTJNo4TKFqSMOjnx5pi1apxiuTqeR+g@mail.gmail.com
Whole thread Raw
In response to Re: Query optimiser is not using 'not null' constraint when 'orderby nulls last' clause is used  (Laurenz Albe <laurenz.albe@cybertec.at>)
Responses Re: Query optimiser is not using 'not null' constraint when 'order by nulls last' clause is used
List pgsql-performance
Hi,

On 2 Feb 2018 15:06, "Laurenz Albe" <laurenz.albe@cybertec.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

pgsql-performance by date:

Previous
From: Vitaliy Garnashevich
Date:
Subject: Re: effective_io_concurrency on EBS/gp2
Next
From: Tom Lane
Date:
Subject: Re: Query optimiser is not using 'not null' constraint when 'order by nulls last' clause is used