Re: index scan forward vs backward = speed difference of 357X slower! - Mailing list pgsql-performance

From Tom Lane
Subject Re: index scan forward vs backward = speed difference of 357X slower!
Date
Msg-id 29203.1328729468@sss.pgh.pa.us
Whole thread Raw
In response to index scan forward vs backward = speed difference of 357X slower!  (Kevin Traster <ktraster@freshgrillfoods.com>)
Responses Re: index scan forward vs backward = speed difference of 357X slower!
List pgsql-performance
Kevin Traster <ktraster@freshgrillfoods.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

pgsql-performance by date:

Previous
From: Ofer Israeli
Date:
Subject: Re: Inserts or Updates
Next
From: Ofer Israeli
Date:
Subject: Vacuuming problems on TOAST table