Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 174CA9FB832 for ; Mon, 7 May 2007 10:45:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 54277-07 for ; Mon, 7 May 2007 10:45:28 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from heaven.kostyrka.org (h1634.serverkompetenz.net [81.169.154.44]) by postgresql.org (Postfix) with ESMTP id 4FE399FB85C for ; Mon, 7 May 2007 10:45:28 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by heaven.kostyrka.org (Postfix) with ESMTP id 70C374FBE3; Mon, 7 May 2007 15:45:25 +0200 (CEST) Received: from heaven.kostyrka.org ([127.0.0.1]) by localhost (heaven.kostyrka.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 12094-04; Mon, 7 May 2007 15:45:25 +0200 (CEST) Received: from [192.168.2.33] (85-124-81-174.dynamic.xdsl-line.inode.at [85.124.81.174]) by heaven.kostyrka.org (Postfix) with ESMTP id 1E6064FB63; Mon, 7 May 2007 15:45:25 +0200 (CEST) Message-ID: <463F2D42.4020709@kostyrka.org> Date: Mon, 07 May 2007 15:44:34 +0200 From: Andreas Kostyrka User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.10) Gecko/20070329 Thunderbird/1.5.0.10 Mnenhy/0.7.5.0 MIME-Version: 1.0 To: "Ambrus Wagner (IJ/ETH)" , pgsql-performance@postgresql.org Subject: Re: Merging large volumes of data References: <53CCFDD6E346CB43994852666C210E91C7AEE8@esealmw116.eemea.ericsson.se> In-Reply-To: <53CCFDD6E346CB43994852666C210E91C7AEE8@esealmw116.eemea.ericsson.se> X-Enigmail-Version: 0.94.2.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/68 X-Sequence-Number: 24458 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I think you'll have to stick with doing your sorting (or merging) in your client. Don't think that PG recognizes the fact it's just a merge step. Andreas Ambrus Wagner (IJ/ETH) wrote: > Dear All, > > I have several tables containing data sorted by 2 keys (neither are keys in db terms (not unique), however). I would like to retrieve all rows from all tables sorted by the same keys, essentially merging the contents of the tables together. While I am completely aware of sort order not being a (fundamental) property of an RDBMS table, I am also aware of indices and clustering (in fact, data is inserted into the tables into the correct order, and not consequently modified in any way). I have a union query like this one: > > select a,b,c,d,e from table1 union all > select a,b,c,d,e from table2 union all > etc... > select a,b,c,d,e from tablen order by a,b; > > Is there a way to prevent PostgreSQL from doing a full sort on the result set after the unions have been completed? Even if I write > > (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. > > Is there a workaround for this within PostgreSQL (another type of query, parameter tuning, stored procedure, anything) or should I use my back-up plan of making separate queries and merging the results in the target language? > > Thanks a lot, > Ambrus > > -- > Wagner, Ambrus (IJ/ETH/GBD) > Tool Designer > GSDC Hungary > > Location: Science Park, A2 40 008 > Phone: +36 1 439 5282 > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGPy1CHJdudm4KnO0RAuKlAKCbYu2G/MYfmX9gAlSxkzA6KB4A+QCeIlAT USxhGD5XL7oGlIh+i2rVyN4= =APcb -----END PGP SIGNATURE-----