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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

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

[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux