Re: index scan forward vs backward = speed difference of 357X slower!

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kevin Traster <ktraster(at)freshgrillfoods(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: index scan forward vs backward = speed difference of 357X slower!
Date: 2012-02-08 19:31:08
Message-ID: 29203.1328729468@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Kevin Traster <ktraster(at)freshgrillfoods(dot)com> writes:
> The query plan and estimates are exactly the same, except desc has index
> scan backwards instead of index scan for changes_shareschange.
> Yet, actual runtime performance is different by 357x slower for the
> ascending version instead of descending.

Apparently, there are some rows passing the filter condition that are
close to the end of the index, but none that are close to the start.
So it takes a lot longer to find the first 15 matches in one case than
the other. You haven't shown us the index definition, but I gather from
the fact that the scan condition is just a Filter (not an Index Cond)
that the index itself doesn't offer any clue as to whether a given row
meets those conditions. So this plan is going to be doing a lot of
random-access heap probes until it finds a match.

> Why and how do I fix it?

Probably, you need an index better suited to the query condition.
If you have one and the problem is that the planner's not choosing it,
then this is going to take more information to resolve.

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Ofer Israeli 2012-02-08 19:33:59 Vacuuming problems on TOAST table
Previous Message Ofer Israeli 2012-02-08 19:22:48 Re: Inserts or Updates