Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 9256C634338 for ; Sat, 28 Feb 2009 12:20:39 -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 95883-05 for ; Sat, 28 Feb 2009 12:20:34 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f170.google.com (mail-bw0-f170.google.com [209.85.218.170]) by mail.postgresql.org (Postfix) with ESMTP id AB845632A32 for ; Sat, 28 Feb 2009 12:20:23 -0400 (AST) Received: by bwz18 with SMTP id 18so1975748bwz.19 for ; Sat, 28 Feb 2009 08:20:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=Crc3+puqY3tItsImm+K+jqJIppraxo/ZG0YeItpFEtw=; b=nZuT4rIfIVP16zGc+I1iXoD0OXFsjUWmqcKWtPJSx5OCL07WvSdZvfiVtFPtfFT904 J9UckT/IN92E+hNC21jgB0nsEdl1YQGvCDyRPXhBzBmCVlVeVKOa7S/vPnH7H4xiVCca GbHXXjtXspZRBKtGEzVTZkwPsWaeI18ZTxt3Y= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=I7tfPLiM4t7av+XiRrci/WWBmNe20u/V++pVXXRfjAOBzstWD2uy11xB6lFeC+QbTO tF+SUSe5Ry4Zna43tTuVnYYH0a3cxsS9iXiAqGV5/9QyyquwH/duEx5z4Rnts/tc7ZaN uqLpN8cTKfguPvCqqIAXW8G9AdbjKZn1G/Xkw= MIME-Version: 1.0 Received: by 10.181.11.3 with SMTP id o3mr1332867bki.172.1235838022408; Sat, 28 Feb 2009 08:20:22 -0800 (PST) In-Reply-To: <603c8f070902271454h5a78a063kcf2b78a10fd956b1@mail.gmail.com> References: <88daf38c0902141425s6972d849i9dad904f55d93c77@mail.gmail.com> <88daf38c0902150845y2878fcbfmcd06d426b53a886a@mail.gmail.com> <88daf38c0902271218p1931ec9dyfdc269bceab24190@mail.gmail.com> <603c8f070902271454h5a78a063kcf2b78a10fd956b1@mail.gmail.com> Date: Sat, 28 Feb 2009 17:20:22 +0100 X-Google-Sender-Auth: 9ed0a693415e138a Message-ID: <88daf38c0902280820p745ea234s85efa7f5e58bd0ed@mail.gmail.com> Subject: Re: Bad plan for nested loop + limit From: Alexander Staubo To: Robert Haas Cc: David Wilson , pgsql-performance Content-Type: text/plain; charset=UTF-8 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/338 X-Sequence-Number: 32919 On Fri, Feb 27, 2009 at 11:54 PM, Robert Haas 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. =C2=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. Alexander.