Re: Interesting query plan change linked to the LIMIT parameter

From: "David Wilson" <david(dot)t(dot)wilson(at)gmail(dot)com>
To: Yannick Le Guédart <yannick(at)over-blog(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Interesting query plan change linked to the LIMIT parameter
Date: 2009-01-20 16:28:33
Message-ID: e7f9235d0901200828x4aab0e66x1e38e6b4b1ce3d6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, Jan 20, 2009 at 10:45 AM, Yannick Le Guédart
<yannick(at)over-blog(dot)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(dot)t(dot)wilson(at)gmail(dot)com

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Yannick Le Guédart 2009-01-21 07:37:50 Re: Interesting query plan change linked to the LIMIT parameter
Previous Message Yannick Le Guédart 2009-01-20 15:45:45 Interesting query plan change linked to the LIMIT parameter