Huge difference between ASC and DESC ordering

From: twoflower <standa(dot)kurik(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Huge difference between ASC and DESC ordering
Date: 2017-03-06 14:22:26
Message-ID: 1488810146219-5947712.post@n3.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

I have the following query

which takes 90 seconds to finish. *JOB_MEMORY* has 45 million rows,
*JOB_MEMORY_STORAGE* has 50 000 rows.

Query plan:

As you can see, it is indeed using an index *JOB_MEMORY_id_desc* in a
backward direction, but it is very slow.

When I change ordering to *desc* in the query, the query finishes
immediately and the query plan is

There is also an index on *JOB_MEMORY.id*. I also tried a composite index on
*(fk_id_storage, id)*, but it did not help (and was not actually used).
I ran *ANALYZE* on both tables.

Postgres 9.6.2, Ubuntu 14.04, 192 GB RAM, SSD, shared_buffers = 8196 MB.
How can I help Postgres execute the query with *asc* ordering as fast as the
one with *desc*?

Thank you.

--
View this message in context: http://www.postgresql-archive.org/Huge-difference-between-ASC-and-DESC-ordering-tp5947712.html
Sent from the PostgreSQL - performance mailing list archive at Nabble.com.

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Jeff Janes 2017-03-06 16:19:31 Re: Huge difference between ASC and DESC ordering
Previous Message Piotr Gasidło 2017-03-06 13:20:42 Performance issue after upgrading from 9.4 to 9.6