X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 91D8C52800 for ; Wed, 8 Jun 2005 11:39:41 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43322-09 for ; Wed, 8 Jun 2005 14:39:24 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 93A0552997 for ; Wed, 8 Jun 2005 11:39:23 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j58E3ZxB001276; Wed, 8 Jun 2005 10:03:35 -0400 (EDT) To: Kim Bisgaard Cc: pgsql-performance@postgresql.org Subject: Re: full outer performance problem In-reply-to: <42A6BC64.4010001@dmi.dk> References: <42A6BC64.4010001@dmi.dk> Comments: In-reply-to Kim Bisgaard message dated "Wed, 08 Jun 2005 11:37:40 +0200" Date: Wed, 08 Jun 2005 10:03:35 -0400 Message-ID: <1275.1118239415@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/170 X-Sequence-Number: 12807 Kim Bisgaard writes: > SELECT station_id, timeobs,temp_grass, temp_dry_at_2m > FROM temp_dry_at_2m a > FULL OUTER JOIN temp_grass b > USING (station_id, timeobs) > WHERE station_id = 52981 > AND timeobs = '2004-1-1 0:0:0' > explain analyse SELECT b.station_id, b.timeobs,temp_grass, temp_dry_at_2m > FROM temp_dry_at_2m a > FULL OUTER JOIN temp_grass b > USING (station_id, timeobs) > WHERE b.station_id = 52981 > AND b.timeobs = '2004-1-1 0:0:0' > Why will PostgreSQL not use the same plan for both these queries - they > are virtually identical?? Because they're semantically completely different. The second query is effectively a RIGHT JOIN, because join rows in which b is all-null will be thrown away by the WHERE. The optimizer sees this (note your second plan doesn't use a Full Join step anywhere) and is able to produce a much better plan. Full outer join is difficult to optimize, in part because we have no choice but to use a merge join for it --- the other join types don't support full join. regards, tom lane