Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 4CB1B9F995D for ; Fri, 26 Oct 2007 18:12:58 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 98800-02 for ; Fri, 26 Oct 2007 18:12:53 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from oxford.xeocode.com (unknown [87.127.95.196]) by postgresql.org (Postfix) with ESMTP id A72C09FA5F6 for ; Fri, 26 Oct 2007 18:12:39 -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 1IlWTt-0003PR-Qj; Fri, 26 Oct 2007 22:12:37 +0100 To: "Pablo Alcaraz" Cc: Subject: Re: Speed difference between select ... union select ... and select from partitioned_table In-Reply-To: <47225014.5040101@laotraesquina.com.ar> (Pablo Alcaraz's message of "Fri\, 26 Oct 2007 16\:37\:40 -0400") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 2960) References: <47225014.5040101@laotraesquina.com.ar> From: Gregory Stark Organization: EnterpriseDB Date: Fri, 26 Oct 2007 22:12:37 +0100 Message-ID: <87tzod1sxm.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/311 X-Sequence-Number: 27333 "Pablo Alcaraz" writes: > Hi List! > > I executed 2 equivalents queries. The first one uses a union structure. The > second uses a partitioned table. The tables are the same with 30 millions of > rows each one and the returned rows are the same. > > But the union query perform faster than the partitioned query. > > My question is: why? :) > > [pabloa@igor testeo]$ cat query-union.sql > select e, p, sum( c) as c > from ( > select e, p, count( *) as c > from tt_00003 > group by e, p > union > select e, p, count( *) as c > from tt_00006 > group by e, p > union ... You should send along the "explain analyze" results for both queries, otherwise we're just guessing. Also, you should consider using UNION ALL instead of plain UNION. Finally you should consider removing all the intermediate GROUP BYs and just group the entire result. In theory it should be faster but in practice I'm not sure it works out that way. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com