X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id D68FED1C4BC for ; Fri, 25 Jul 2003 13:45:13 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 37934-10 for ; Fri, 25 Jul 2003 10:44:30 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id D8EEED1C4B7 for ; Fri, 25 Jul 2003 10:45:11 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h6PDjFQ3008077; Fri, 25 Jul 2003 09:45:15 -0400 (EDT) To: oheinz@stud.fbi.fh-darmstadt.de Cc: Dmitry Tkach , Christoph Haller , pgsql-sql@postgresql.org Subject: Re: Problem using Subselect results In-reply-to: <1059123702.3f20f1f689c61@stud.fbi.fh-darmstadt.de> References: <3F1FCCE2.47ACF401@rodos.fzk.de> <1059051709.3f1fd8bd630bc@stud.fbi.fh-darmstadt.de> <3F1FF331.7000106@openratings.com> <1059123702.3f20f1f689c61@stud.fbi.fh-darmstadt.de> Comments: In-reply-to oheinz@stud.fbi.fh-darmstadt.de message dated "Fri, 25 Jul 2003 11:01:42 +0200" Date: Fri, 25 Jul 2003 09:45:15 -0400 Message-ID: <8076.1059140715@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/376 X-Sequence-Number: 14490 oheinz@stud.fbi.fh-darmstadt.de writes: > Quoting Dmitry Tkach : >> CREATE VIEW my_view AS SELECT b,c from >> (SELECT a, b FROM table1 WHERE b=1) as my_ab, >> (SELECT a, c FROM table2) as my_ac WHERE my_ac.a=my_ab.a > I assume that with this statement postgresql will compute both subselects, do > a cross join on both results an then reduce them to those who match the > condition my_ac.a=my_ab.a, right? No, it's smarter than that. I tried the experiment in 7.3 and CVS tip, using some tables from the regression database: regression=# create view my_view as select b,c from regression-# (select unique1,unique2 from tenk1 where unique2=1) as regression-# my_ab(a,b), regression-# (select unique1,unique2 from onek) as my_ac(a,c) regression-# where my_ac.a = my_ab.a; CREATE VIEW regression=# explain select * from my_view; QUERY PLAN --------------------------------------------------------------------------------- Nested Loop (cost=0.00..24.47 rows=1 width=16) -> Index Scan using tenk1_unique2 on tenk1 (cost=0.00..3.01 rows=1 width=8) Index Cond: (unique2 = 1) -> Index Scan using onek_unique1 on onek (cost=0.00..21.40 rows=5 width=8) Index Cond: (onek.unique1 = "outer".unique1) (5 rows) regression=# Looks like a fairly decent plan to me. It's certainly not letting the sub-select structure get in its way. regards, tom lane