X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B3CB8DB616 for ; Thu, 17 Nov 2005 14:06:59 -0400 (AST) 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 82445-02 for ; Thu, 17 Nov 2005 18:06:58 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 133D7DB38D for ; Thu, 17 Nov 2005 14:06:56 -0400 (AST) 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 jAHI6t3T029532; Thu, 17 Nov 2005 13:06:55 -0500 (EST) To: Rich Doughty cc: Pgsql-Performance Subject: Re: Strange query plan invloving a view In-reply-to: <437BD7A2.608@opusvl.com> References: <437BD7A2.608@opusvl.com> Comments: In-reply-to Rich Doughty message dated "Thu, 17 Nov 2005 01:06:42 +0000" Date: Thu, 17 Nov 2005 13:06:55 -0500 Message-ID: <29531.1132250815@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.006 required=5 tests=[AWL=0.006] X-Spam-Score: 0.006 X-Spam-Level: X-Archive-Number: 200511/322 X-Sequence-Number: 15579 Rich Doughty writes: > However, the following query (which i believe should be equivalent) > SELECT * > FROM > tokens.ta_tokenhist h INNER JOIN > tokens.ta_tokens t ON h.token_id = t.token_id LEFT JOIN > tokens.ta_tokenhist i ON t.token_id = i.token_id AND > i.status = 'issued' LEFT JOIN > tokens.ta_tokenhist s ON t.token_id = s.token_id AND > s.status = 'sold' LEFT JOIN > tokens.ta_tokenhist r ON t.token_id = r.token_id AND > r.status = 'redeemed' > WHERE > h.sarreport_id = 9 > ; No, that's not equivalent at all, because the implicit parenthesization is left-to-right; therefore you've injected the constraint to a few rows of ta_tokenhist (and therefore only a few rows of ta_tokens) into the bottom of the LEFT JOIN stack. In the other case the constraint is at the wrong end of the join stack, and so the full view output gets formed before anything gets thrown away. Some day the Postgres planner will probably be smart enough to rearrange the join order despite the presence of outer joins ... but today is not that day. regards, tom lane