Re: Reversing NULLS in ORDER causes index not to be used?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
Cc: pgsql-performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Reversing NULLS in ORDER causes index not to be used?
Date: 2020-12-19 02:02:59
Message-ID: 226299.1608343379@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com> writes:
> Hi. I'm wondering if this is normal or at least known behavior?
> Basically, if I'm specifying a LIMIT and also NULLS FIRST (or NULLS LAST
> with a descending sort), I get a sequence scan and a couple of orders of
> magnitude slower query. Perhaps not relevantly, but definitely ironically,
> the sort field in question is defined to be NOT NULL.

The index won't get credit for matching the requested ordering if it's
got the wrong null-ordering polarity. There's not an exception for
NOT NULL columns. If you know the column hasn't got nulls, why are
you bothering with a nondefault null-ordering request?

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Ken Tanzer 2020-12-19 02:30:16 Re: Reversing NULLS in ORDER causes index not to be used?
Previous Message Ken Tanzer 2020-12-19 01:53:03 Reversing NULLS in ORDER causes index not to be used?