Re: Problem using Subselect results

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: oheinz(at)stud(dot)fbi(dot)fh-darmstadt(dot)de
Cc: Dmitry Tkach <dmitry(at)openratings(dot)com>, Christoph Haller <ch(at)rodos(dot)fzk(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Problem using Subselect results
Date: 2003-07-25 13:45:15
Message-ID: 8076.1059140715@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

oheinz(at)stud(dot)fbi(dot)fh-darmstadt(dot)de writes:
> Quoting Dmitry Tkach <dmitry(at)openratings(dot)com>:
>> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Dmitry Tkach 2003-07-25 14:23:29 Re: Problem using Subselect results
Previous Message Elielson Fontanezi 2003-07-25 13:08:16 Query analyse