Re: Interesting query plan change linked to the LIMIT parameter - Mailing list pgsql-performance

From David Wilson
Subject Re: Interesting query plan change linked to the LIMIT parameter
Date
Msg-id e7f9235d0901200828x4aab0e66x1e38e6b4b1ce3d6@mail.gmail.com
Whole thread Raw
In response to Interesting query plan change linked to the LIMIT parameter  ("Yannick Le Guédart" <yannick@over-blog.com>)
Responses Re: Interesting query plan change linked to the LIMIT parameter  (Yannick Le Guédart <yannick@over-blog.com>)
List pgsql-performance
On Tue, Jan 20, 2009 at 10:45 AM, Yannick Le Guédart
<yannick@over-blog.com> wrote:

>
> The second query scans the whole comment table which is very dangerous for
> production servers.

That's not quite true. The second does an index scan- the planner
seems to be guessing that it'll fulfill the required limit early in
the index scan; only with a pathologically bad case would it actually
have to scan the entire thing. Basically, the second query is
optimized to spit out the first few rows quickly, since that's all you
asked for with the limit.

Note that your first query has a final cost estimate of "Limit
(cost=10261.19..10263.69 rows=1000 width=8)", indicating an estimated
10261.19 to emit the first row; the second has "Limit
(cost=0.00..3588.42 rows=1 width=8)" estimating 0.00 (basically,
instant) to emit the first - and only desired - row.

That all said, an explain analyze would give us a better idea of
what's going on- we can't tell if the planner is making bad estimates
without the knowledge of what the real timing and row count results of
plan stages were.


--
- David T. Wilson
david.t.wilson@gmail.com

pgsql-performance by date:

Previous
From: "Yannick Le Guédart"
Date:
Subject: Interesting query plan change linked to the LIMIT parameter
Next
From: Yannick Le Guédart
Date:
Subject: Re: Interesting query plan change linked to the LIMIT parameter