Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C3D6F4767D2 for ; Wed, 22 Jan 2003 17:23:26 -0500 (EST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 16AC5476F83 for ; Wed, 22 Jan 2003 16:35:41 -0500 (EST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.6/8.12.6) with ESMTP id h0MLZX5u024369; Wed, 22 Jan 2003 16:35:33 -0500 (EST) To: Stephan Szabo Cc: Tomasz Myrta , Jan Wieck , Ries van Twisk , pgsql-sql@postgresql.org Subject: Re: To use a VIEW or not to use a View..... In-reply-to: <20030122090117.D97465-100000@megazone23.bigpanda.com> References: <20030122090117.D97465-100000@megazone23.bigpanda.com> Comments: In-reply-to Stephan Szabo message dated "Wed, 22 Jan 2003 09:09:14 -0800" Date: Wed, 22 Jan 2003 16:35:33 -0500 Message-ID: <24368.1043271333@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/346 X-Sequence-Number: 11211 Stephan Szabo writes: > ... but this is a general transitivity constraint AFAIK, not > one actually to do with views (ie, if you wrote out the query without a > view, you can run into the same issue). It's somewhat easier to run into > the case with views and the effect may be exasperated by views, but it's > a general condition. Right. Views are just macros --- they don't in themselves affect the planner's ability to generate a good plan. But they make it easier to generate baroque queries without thinking much about what you're doing, and in complex queries the planner doesn't always make the deductions and simplifications that are obvious to a human. > For example: > create table a(a int); > create table c(a int); > sszabo=# explain select * from a join c using (a) where a=3; > QUERY PLAN > ------------------------------------------------------------- > Hash Join (cost=1.01..26.08 rows=6 width=8) > Hash Cond: ("outer".a = "inner".a) > -> Seq Scan on c (cost=0.00..20.00 rows=1000 width=4) > -> Hash (cost=1.01..1.01 rows=1 width=4) > -> Seq Scan on a (cost=0.00..1.01 rows=1 width=4) > Filter: (a = 3) > (6 rows) > The filter is applied only to a. So, if you really wanted the > c.a=3 condition to be applied for whatever reason you're out of > luck. FWIW, CVS tip is brighter: the condition does propagate to both relations. Hash Join (cost=22.51..45.04 rows=1 width=8) Hash Cond: ("outer".a = "inner".a) -> Seq Scan on a (cost=0.00..22.50 rows=5 width=4) Filter: (a = 3) -> Hash (cost=22.50..22.50 rows=5 width=4) -> Seq Scan on c (cost=0.00..22.50 rows=5 width=4) Filter: (3 = a) The reason this is useful is that (a) fewer rows need to be joined, and (b) we may be able to make effective use of indexes on both tables. regards, tom lane