pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Robert Haas <robertmhaas@gmail.com>
To: Alexander Staubo <alex@bengler.no>
Cc: David Wilson <david.t.wilson@gmail.com>
Cc: pgsql-performance <pgsql-performance@postgresql.org>
Subject: Re: Bad plan for nested loop + limit
Date: Sat, 28 Feb 2009 22:32:57 -0500
Message-ID: <603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com> (raw)
In-Reply-To: <88daf38c0902280820p745ea234s85efa7f5e58bd0ed@mail.gmail.com>
References: <88daf38c0902141425s6972d849i9dad904f55d93c77@mail.gmail.com>
	<e7f9235d0902142029h5b53baf2gede92b22afbbad35@mail.gmail.com>
	<88daf38c0902150845y2878fcbfmcd06d426b53a886a@mail.gmail.com>
	<88daf38c0902271218p1931ec9dyfdc269bceab24190@mail.gmail.com>
	<603c8f070902271454h5a78a063kcf2b78a10fd956b1@mail.gmail.com>
	<88daf38c0902280820p745ea234s85efa7f5e58bd0ed@mail.gmail.com>

On Sat, Feb 28, 2009 at 11:20 AM, Alexander Staubo <alex@bengler.no> wrote:
> On Fri, Feb 27, 2009 at 11:54 PM, Robert Haas <robertmhaas@gmail.com> wrote:
>> The problem here is that the planner estimates the cost of a Limit
>> plan node by adding up (1) the startup cost of the underlying plan
>> node, in this case 0 for the nestjoin, and (2) a percentage of the run
>> cost, based on the ratio of the number of rows expected to be returned
>> to the total number of rows.  In this case, the nested loop is
>> expected to return 6944 rows, so it figures it won't have to get very
>> far to find the 4 you requested.
> [...]
>> I will think about this some more but nothing is occurring to me off
>> the top of my head.
>
> Thanks for explaining. Is there any way to rewrite the query in a way
> that will avoid the nested loop join -- other than actually disabling
> nested loop joins? If I do the latter, the resulting query uses a hash
> join and completes in 80-100 ms, which is still pretty horrible,
> especially for a query that returns nothing, but extremely auspicious
> compared to the unthinkable 4-5 seconds for the current query.

Can you post the schema for the two tables in question?  Feel free to
omit any columns that aren't included in the query, but make sure to
include any unique indices, etc.

What do you have default_statistics_target set to?  If it's less than
100, you should probably raise it to 100 and re-analyze (the default
value for 8.4 will be 100, but for 8.3 and prior it is 10).

What is the approximate total number of rows in each of these two
tables?  Of the rows in section_items, how many have subject_type =
'Event'?

...Robert



view thread (8+ messages)  latest in thread

Message-ID: <603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com>
Permalink:  ../603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com/
Also on:    postgresql.org/message-id/603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: robertmhaas@gmail.com, alex@bengler.no, david.t.wilson@gmail.com
  Subject: Re: Bad plan for nested loop + limit
  In-Reply-To: <603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com>
  References: <88daf38c0902141425s6972d849i9dad904f55d93c77@mail.gmail.com> <e7f9235d0902142029h5b53baf2gede92b22afbbad35@mail.gmail.com> <88daf38c0902150845y2878fcbfmcd06d426b53a886a@mail.gmail.com> <88daf38c0902271218p1931ec9dyfdc269bceab24190@mail.gmail.com> <603c8f070902271454h5a78a063kcf2b78a10fd956b1@mail.gmail.com> <88daf38c0902280820p745ea234s85efa7f5e58bd0ed@mail.gmail.com> <603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox