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 1A3C8DB420 for ; Sun, 30 Oct 2005 14:27:17 -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 92881-05 for ; Sun, 30 Oct 2005 18:27:13 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 54430DB411 for ; Sun, 30 Oct 2005 14:27:13 -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 j9UIR1HV013994; Sun, 30 Oct 2005 13:27:01 -0500 (EST) To: Svenne Krap Cc: PgSQL Performance Subject: Re: multi-layered view join performance oddities In-reply-to: <4364FFD4.2020801@krap.dk> References: <4364FFD4.2020801@krap.dk> Comments: In-reply-to Svenne Krap message dated "Sun, 30 Oct 2005 18:16:04 +0100" Date: Sun, 30 Oct 2005 13:27:01 -0500 Message-ID: <13993.1130696821@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: 200510/463 X-Sequence-Number: 15231 Svenne Krap writes: > create view ord_institutes_sum as > SELECT ord_property_type_all.dataset_id, ord_property_type_all.nb_property_type_id, 0 AS institut, sum(ord_property_type_all.amount) AS amount > FROM ord_property_type_all > GROUP BY ord_property_type_all.dataset_id, ord_property_type_all.nb_property_type_id; > create view ord_result_pct as > SELECT t1.dataset_id, t1.nb_property_type_id, t1.institut, t1.amount / t2.amount * 100::numeric AS pct > FROM ord_property_type_all t1, ord_institutes_sum t2 > WHERE t1.dataset_id = t2.dataset_id AND t1.nb_property_type_id = t2.nb_property_type_id; This is really pretty horrid code: you're requesting double evaluation of the ord_property_type_all view, and then joining the two calculations to each other. No, the planner will not detect how silly this is :-(, nor will it realize that there's guaranteed to be a match for every row --- I believe the latter is the reason for the serious misestimation that Steinar noted. The misestimation doesn't hurt particularly when evaluating ord_result_pct by itself, because there are no higher-level decisions to make ... but it hurts a lot when you join ord_result_pct to some other stuff. It seems like there must be a way to get the percentage amounts with only one evaluation of ord_property_type_all, but I'm not seeing it right offhand. regards, tom lane