Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 4351B9FB6E6 for ; Mon, 7 May 2007 11:24:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 60670-04 for ; Mon, 7 May 2007 11:24:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from oxford.xeocode.com (unknown [87.127.95.194]) by postgresql.org (Postfix) with ESMTP id 0B0399FB878 for ; Mon, 7 May 2007 11:24:31 -0300 (ADT) Received: from localhost.enta.net ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.67) (envelope-from ) id 1Hl48W-0000Sm-Dd; Mon, 07 May 2007 15:24:24 +0100 From: Gregory Stark To: "Andreas Kostyrka" Cc: "Ambrus Wagner \(IJ/ETH\)" , Subject: Re: Merging large volumes of data In-Reply-To: <463F2D42.4020709@kostyrka.org> (Andreas Kostyrka's message of "Mon, 07 May 2007 15:44:34 +0200") Organization: EnterpriseDB References: <53CCFDD6E346CB43994852666C210E91C7AEE8@esealmw116.eemea.ericsson.se> <463F2D42.4020709@kostyrka.org> X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 191) Date: Mon, 07 May 2007 15:24:23 +0100 Message-ID: <87lkg0yanc.fsf@oxford.xeocode.com> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/69 X-Sequence-Number: 24459 "Andreas Kostyrka" writes: >> (select a,b,c,d,e from table1 order by a,b) union all >> (select a,b,c,d,e from table2 order by a,b) union all >> etc... >> (select a,b,c,d,e from tablen order by a,b) order by a,b; >> >> PostgreSQL does not seem to realise (maybe it should not be able to do this >> trick anyway) that the last "order by" clause is merely a final merge step >> on the ordered data sets. There's no plan type in Postgres for merging pre-sorted data like this. The only merge plan type is for joins which isn't going to be what you need. But the queries as written here would be just as fast or faster to do one big sort as they would be to do separate sorts and merge the results. You might want to do it the way you describe if there were selective WHERE clauses that you've left out that make the intermediate orderings come for free. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com