Re: Slow query - possible bug? - Mailing list pgsql-performance

From Tom Lane
Subject Re: Slow query - possible bug?
Date
Msg-id 14985.1145381508@sss.pgh.pa.us
Whole thread Raw
In response to Re: Slow query - possible bug?  (Gavin Hamill <gdh@laterooms.com>)
Responses Re: Slow query - possible bug?  (Gavin Hamill <gdh@laterooms.com>)
List pgsql-performance
Gavin Hamill <gdh@laterooms.com> writes:
> Tom Lane wrote:
>> I'm thinking the planner is misestimating something, but it's hard
>> to tell what without breaking it down.

> (allocation0_."Date" between '2006-06-10 00:00:00.000000' and
> '2006-06-10 00:00:00.000000');
>                                                                  QUERY PLAN
>
-------------------------------------------------------------------------------------------------------------------------------------------
>  Index Scan using ix_date on "Allocation" allocation0_  (cost=0.00..4.77
> rows=1 width=34) (actual time=0.035..6706.467 rows=34220 loops=1)
>    Index Cond: (("Date" >= '2006-06-10'::date) AND ("Date" <=
> '2006-06-10'::date))
>  Total runtime: 6728.743 ms

Bingo, there's our misestimation: estimated 1 row, actual 34220 :-(

That's why it's choosing the wrong index: it thinks the condition on
RoomID isn't going to reduce the number of rows fetched any further,
and so the smaller index ought to be marginally cheaper to use.
In reality, it works way better when using the two-column index.

I think this is the same problem recently discussed about how the
degenerate case for a range comparison is making an unreasonably small
estimate, where it probably ought to fall back to some equality estimate
instead.  With the simple-equality form of the date condition, it does
get a reasonable estimate, and so it picks the right index.

There should be a fix for this by the time PG 8.2 comes out, but in the
meantime you might find that it helps to write the range check in a way
that doesn't have identical bounds, eg
    date >= '2006-06-10'::date AND date < '2006-06-11'::date

            regards, tom lane

pgsql-performance by date:

Previous
From: "Rodrigo Sakai"
Date:
Subject: Re: FOREIGN KEYS vs PERFORMANCE
Next
From: Gavin Hamill
Date:
Subject: Re: Slow query - possible bug?