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 56814DAFFF for ; Tue, 22 Nov 2005 09:29:33 -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 34544-07 for ; Tue, 22 Nov 2005 13:29:35 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id 7713DDAFE3 for ; Tue, 22 Nov 2005 09:29:30 -0400 (AST) Received: from europa.cosmos.opusvl.com (europa.cosmos.opusvl.com [213.106.249.125]) by svr2.postgresql.org (Postfix) with ESMTP id 3AC91F0BA2 for ; Tue, 22 Nov 2005 13:29:34 +0000 (GMT) Received: from localhost.localdomain ([127.0.0.1]) by europa.cosmos.opusvl.com with esmtp (Exim 4.30) id 1EeYDB-0001vy-ET; Tue, 22 Nov 2005 13:29:29 +0000 Message-ID: <43831D39.4030407@opusvl.com> Date: Tue, 22 Nov 2005 13:29:29 +0000 From: Rich Doughty Organization: Opus VL User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tom Lane Cc: Pgsql-Performance Subject: Re: Strange query plan invloving a view References: <437BD7A2.608@opusvl.com> <29531.1132250815@sss.pgh.pa.us> In-Reply-To: <29531.1132250815@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/437 X-Sequence-Number: 15694 Tom Lane wrote: > 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. thanks for the reply. is there any way i can achieve what i need to by using views, or should i just use a normal query? i'd prefer to use a view but i just can't get round the performance hit. -- - Rich Doughty