15

I have a large table (about 11,000,000 rows) and I need to find the first item given a sorting condition.

Note that column Date does NOT accept nulls

Why isn't Postgres using the index:

CREATE INDEX track_ix_date
  ON "Track"
  USING btree
  ("Date" DESC NULLS LAST);

On this simple query:

select * from "Track" order by "Date" desc limit 1

But it does use it on this other query:

select * from "Track" order by "Date" desc nulls last limit 1

The second query is in fact much more faster that the first query.

I have read the indexes and ORDER BY documentation and says that in the special case of an ORDER BY with a LIMIT clause is much more efficient to use the index instead of scanning the table, because the sorting would need to scan the full table just to get a single item

Shouldn't Postgres detect that nulls last / first doesn't matter since the column doesn't accept nulls and just use the fastest method?

3
  • Why are you specifying NULLS LAST at all if you know that your DATE column is NOT NULL?
    – Nick
    Commented Nov 1, 2016 at 17:56
  • Because I noted that the index scan was using the condition DATE IS NOT NULL so I put the NULLS LAST by thinking that postgre was checking that condition because when ordering in descending order the first element was considered as a posible null
    – Rafael
    Commented Nov 1, 2016 at 18:24
  • 1
    Please edit your question and add the the execution plan generated using explain (analyze, verbose). Formatted text please, no screen shots
    – user330315
    Commented Nov 1, 2016 at 18:36

3 Answers 3

4

There is always a tradeoff, because making the optimizer smarter also means making the optimizer slower, which hurts everybody.

Currently, it isn't smart enough, so you'll have to change the index definition or the query to get it to work.

It might be worth asking for such an improvement on the pgsql-hackers mailing list or write a patch for it yourself and submit it there.

1

Because you're doing a "SELECT ALL ROWS" with that query. Postgres doesn't bother using an index.

I would bet that if you added a "WHERE date = ?" condition, the index would be used.

1
  • Nope even if we add where date = ?, it is not using the index
    – Surya
    Commented May 6, 2020 at 17:05
1

I came across this question when I was searching for an answer to a very similar problem. I found the answer in this article in the PostgreSQL documentation. https://www.postgresql.org/docs/current/indexes-ordering.html

So in short: Limit N is an order by function. When there is an index matching the order by clause of the query it can use it, otherwise not. So by specifying "null last" you are matching the order by of the query with the order of the index making it usable. Otherwise the server cannot be certain it is fetching the correct N records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.