Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 54F42632907 for ; Sat, 28 Feb 2009 23:33:17 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 04968-04 for ; Sat, 28 Feb 2009 23:32:59 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from yx-out-2324.google.com (yx-out-2324.google.com [74.125.44.30]) by mail.postgresql.org (Postfix) with ESMTP id 93AD0632796 for ; Sat, 28 Feb 2009 23:32:59 -0400 (AST) Received: by yx-out-2324.google.com with SMTP id 3so1125586yxj.73 for ; Sat, 28 Feb 2009 19:32:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=HQmf1C6YttMjWtd4JS1tfXPtt2LdQQB2DVnF2xYeKLQ=; b=NRcnanO8mzJuCwmULwYFhAsEwOphEv6JLBwA8JXsflHj7YxsfzEj66nA6WnhNtgY/I BkYK2nKdBwxNAcaONgwmX12a2mkYmTPBEvHDL+Pj9yY72P8L3Gjc2mYvX+Z5enXGWcXQ TxDCxOb7jQxW1xoGqiZ9x8L/UBc2FBLffDcoI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=EPWeqvjePH4Vd1BcsWxMzj/EOEzwE1WTFK9V4QQAAC0BsxUEfYqP2ytDLLPzss48Jb f84ziJ2Gi6HycoJnYxyPz+u4l70dTRC0Xo5F4daGxdfAzMcsd2CsLA+Y8G9LQ3lQWhTr vSiVXvh2YD/p6+vMtBLVAcoMjTjTuffJsCOfg= MIME-Version: 1.0 Received: by 10.220.97.147 with SMTP id l19mr1358304vcn.8.1235878377681; Sat, 28 Feb 2009 19:32:57 -0800 (PST) In-Reply-To: <88daf38c0902280820p745ea234s85efa7f5e58bd0ed@mail.gmail.com> References: <88daf38c0902141425s6972d849i9dad904f55d93c77@mail.gmail.com> <88daf38c0902150845y2878fcbfmcd06d426b53a886a@mail.gmail.com> <88daf38c0902271218p1931ec9dyfdc269bceab24190@mail.gmail.com> <603c8f070902271454h5a78a063kcf2b78a10fd956b1@mail.gmail.com> <88daf38c0902280820p745ea234s85efa7f5e58bd0ed@mail.gmail.com> Date: Sat, 28 Feb 2009 22:32:57 -0500 Message-ID: <603c8f070902281932w257853e2me25cf567da6a3c46@mail.gmail.com> Subject: Re: Bad plan for nested loop + limit From: Robert Haas To: Alexander Staubo Cc: David Wilson , pgsql-performance Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200902/341 X-Sequence-Number: 32922 On Sat, Feb 28, 2009 at 11:20 AM, Alexander Staubo wrote: > On Fri, Feb 27, 2009 at 11:54 PM, Robert Haas wro= te: >> 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. =A0In 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 =3D 'Event'? ...Robert