Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uxlok-0000Ls-Nu for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jul 2013 22:27:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Uxloj-0003S7-Sj for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jul 2013 22:27:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uxloi-0003S0-QZ for pgsql-performance@postgresql.org; Fri, 12 Jul 2013 22:27:56 +0000 Received: from mail-ie0-f169.google.com ([209.85.223.169]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxloW-0003hC-Ku for pgsql-performance@postgresql.org; Fri, 12 Jul 2013 22:27:55 +0000 Received: by mail-ie0-f169.google.com with SMTP id 10so22242667ied.28 for ; Fri, 12 Jul 2013 15:27:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=Q1srhPfygH6QeMI0SkhzF6U+zxVJrqVuSetdnmtJN0s=; b=HAEupq4/BSD6VInQY2xQU7NRtXE9TZiY+bpW/5xftBq3N5wxS+mH16LtHqSS8ivdoe yxlb5XL1DDJKqyaOWcHEtzlA76oslHu94OstiLHT3HfwLfMxrZXOrD8z4Y7kQgWuOc9T k7S/uzb5fwtJRRMtCqiCd3R+jEZ4TptgMTF4Mb8+zZd6rHGgkdMN1ZIuOXA/bLbX908C boc9eWxhfFfNwbyLlww72SpMcdI0gCpmEenkrPhWsNdwPO0l/HRFrlVbR+MZHV5Z1WP/ 8Y1hyuRO8VGcd87BbRE++RtRBdO5oSYQR6ZX11oLZ6ZTNcJlXLzoNcOUamIR2IfyIBvl BPSg== X-Received: by 10.42.70.194 with SMTP id g2mr13226579icj.83.1373668062529; Fri, 12 Jul 2013 15:27:42 -0700 (PDT) Received: from brianf.consistentstate.com (216-98-193-98.static.forethought.net. [216.98.193.98]) by mx.google.com with ESMTPSA id hj6sm1866170igb.1.2013.07.12.15.27.41 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 12 Jul 2013 15:27:41 -0700 (PDT) Message-ID: <51E082D8.4030503@consistentstate.com> Date: Fri, 12 Jul 2013 16:27:36 -0600 From: Brian Fehrle User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130311 Thunderbird/17.0.4 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Trying to eliminate union and sort References: <51DEF924.1040809@consistentstate.com> <51DF51DA.7010509@agliodbs.com> In-Reply-To: <51DF51DA.7010509@agliodbs.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQmOcB6y9ZNRRt7yxlDnrw+oQgNCNepmczMuN0d+8f3hXsLqOl8WNu21P2rSllngheHKcWov X-Pg-Spam-Score: -2.6 (--) 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 On 07/11/2013 06:46 PM, Josh Berkus wrote: > 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 I tried this originally, however my resulting rowcount is different. The original query returns 9,955,729 rows This above one returns 7,213,906 As for the counts on the tables: table1 3,653,472 table2 2,191,314 table3 25,676,589 I think it's safe to assume right now that any resulting joins are not one-to-one - Brian F -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance