Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 93A349F9ED7 for ; Wed, 27 Sep 2006 16:01:53 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 47857-03 for ; Wed, 27 Sep 2006 16:01:48 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from lynet.oppetid.no (oppetid.no [80.65.51.2]) by postgresql.org (Postfix) with ESMTP id D88709FA21B for ; Wed, 27 Sep 2006 16:01:47 -0300 (ADT) Received: by lynet.oppetid.no (Postfix, from userid 1019) id 63A5311AFA3; Wed, 27 Sep 2006 21:01:45 +0200 (CEST) Date: Wed, 27 Sep 2006 21:01:45 +0200 From: Tobias Brox To: pgsql-performance@postgresql.org Subject: Re: Merge Join vs Nested Loop Message-ID: <20060927190145.GB28174@oppetid.no> References: <20060926193553.GA27268@oppetid.no> <719.1159308596@sss.pgh.pa.us> <20060927094803.GA11245@oppetid.no> <1159369111.4643.0.camel@localhost.localdomain> <20060927150512.GE17834@oppetid.no> <1159370365.4643.7.camel@localhost.localdomain> <20060927152655.GF17834@oppetid.no> <1159371095.26848.84.camel@state.g2switchworks.com> <20060927153619.GG17834@oppetid.no> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060927153619.GG17834@oppetid.no> Organization: Group Nordicbet User-Agent: Mutt/1.5.9i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.192 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200609/369 X-Sequence-Number: 21055 I found a way to survive yet some more weeks :-) One of the queries we've had most problems with today is principially something like: select A.*,sum(B.*) from A join B where A.created>x and ... order by A.created desc limit 32 group by A.* There is by average two rows in B for every row in A. Note the 'limit 32'-part. I rewrote the query to: select A.*,(select sum(B.*) from B ...) where A.created>x and ... order by A.created desc limit 32; And voila, the planner found out it needed just some few rows from A, and execution time was cutted from 1-2 minutes down to 20 ms. :-) I've also started thinking a bit harder about table partitioning, if we add some more redundancy both to the queries and the database, it may help us drastically reduce the real expenses of some of the merge joins...