Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E80682E3514 for ; Thu, 6 Dec 2007 13:55:47 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 40109-08-2 for ; Thu, 6 Dec 2007 13:55:40 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 1C43C2E351F for ; Thu, 6 Dec 2007 13:55:41 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id lB6Htc3r017457; Thu, 6 Dec 2007 12:55:38 -0500 (EST) To: Matthew cc: pgsql-performance@postgresql.org Subject: Re: TB-sized databases In-reply-to: References: <4544e0330711260904g45d893a8ub1731e245aa8fc84@mail.gmail.com> <13851.1196351131@sss.pgh.pa.us> <1196352866.4246.1304.camel@ebony.site> <200712051507.22745.xzilla@users.sourceforge.net> <1196933896.4255.340.camel@ebony.site> <20071206154212.GP5294@mathom.us> <15753.1196957598@sss.pgh.pa.us> Comments: In-reply-to Matthew message dated "Thu, 06 Dec 2007 17:46:35 +0000" Date: Thu, 06 Dec 2007 12:55:38 -0500 Message-ID: <17456.1196963738@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/83 X-Sequence-Number: 27899 Matthew writes: > ... For this query, Postgres would perform a nested loop, > iterating over all rows in the small table, and doing a hundred index > lookups in the big table. This completed very quickly. However, adding the > LIMIT meant that suddenly a merge join was very attractive to the planner, > as it estimated the first row to be returned within milliseconds, without > needing to sort either table. > The problem is that Postgres didn't know that the first hit in the big > table would be about half-way through, after doing a index sequential scan > for half a bazillion rows. Hmm. IIRC, there are smarts in there about whether a mergejoin can terminate early because of disparate ranges of the two join variables. Seems like it should be straightforward to fix it to also consider whether the time-to-return-first-row will be bloated because of disparate ranges. I'll take a look --- but it's probably too late to consider this for 8.3. regards, tom lane