Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxRVF-0006Ho-JR for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jul 2013 00:46:29 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UxRVE-0007dl-PG for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jul 2013 00:46:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxRVC-0007dc-Is for pgsql-performance@postgresql.org; Fri, 12 Jul 2013 00:46:26 +0000 Received: from smtp.01.com ([199.36.142.181] helo=smtp-out-1.01.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxRV8-0005gJ-6P for pgsql-performance@postgresql.org; Fri, 12 Jul 2013 00:46:26 +0000 Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id F3EA429400D for ; Thu, 11 Jul 2013 19:46:19 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rte7xWIzXoID for ; Thu, 11 Jul 2013 19:46:19 -0500 (CDT) Received: from smtp-out-1.01.com (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id D142D294017 for ; Thu, 11 Jul 2013 19:46:19 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id C4111294010 for ; Thu, 11 Jul 2013 19:46:19 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id MgnWQnZmmB_c for ; Thu, 11 Jul 2013 19:46:19 -0500 (CDT) Received: from [172.47.23.103] (70-36-143-10.dsl.dynamic.sonic.net [70.36.143.10]) by smtp-out-1.01.com (Postfix) with ESMTPSA id 7CC6E29400D for ; Thu, 11 Jul 2013 19:46:19 -0500 (CDT) Message-ID: <51DF51DA.7010509@agliodbs.com> Date: Thu, 11 Jul 2013 17:46:18 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Trying to eliminate union and sort References: <51DEF924.1040809@consistentstate.com> In-Reply-To: X-Enigmail-Version: 1.5.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Brian, > 3. I'm trying to eliminate the union, however I have two problems. > A) I can't figure out how to have an 'or' clause in a single join that > would fetch all the correct rows. If I just do: > LEFT OUTER JOIN table2 t2 ON (t2.real_id = t.id OR t2.real_id = > t.backup_id), I end up with many less rows than the original query. B. > > I believe the issue with this is a row could have one of three > possibilities: > * part of the first query but not the second -> results in 1 row after > the union > * part of the second query but not the first -> results in 1 row after > the union > * part of the first query and the second -> results in 2 rows after the > union (see 'B)' for why) > > B) the third and fourth column in the SELECT will need to be different > depending on what column the row is joined on in the LEFT OUTER JOIN to > table2, so I may need some expensive case when logic to filter what is > put there based on whether that row came from the first join clause, or > the second. No, it doesn't: SELECT t.id, t.mycolumn1, table3.otherid as otherid1, table3a.otherid as otherid2, t.mycolumn2 FROM t LEFT OUTER JOIN table2 ON ( t.id = t2.real_id OR t.backup_id = t2.real_id ) LEFT OUTER JOIN table3 ON ( t.typeid = table3.id ) LEFT OUTER JOIN table3 as table3a ON ( table2.third_id = table3.id ) WHERE t.external_id IN ( ... ) ORDER BY t.mycolumn2, t.id -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance