From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Bruce Lowery <bruce(dot)lowery(at)edventions(dot)com> |
Cc: | "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgreSQL(dot)org> |
Subject: | Re: [SQL] uncorrelated subqueries |
Date: | 1999-07-08 19:30:06 |
Message-ID: | 461.931462206@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Bruce Lowery <bruce(dot)lowery(at)edventions(dot)com> writes:
> In v6.4.2 do uncorrelated subqueries get run on each iteration of the
> outer loop?
It looks like the particular example you give is done that way, but the
system does know about uncorrelated subqueries. For example, using 6.5:
explain SELECT a FROM table1 WHERE table1.b=33 AND
table1.c = ( SELECT d FROM table2 WHERE table2.e=44);
NOTICE: QUERY PLAN:
Seq Scan on table1 (cost=43.00 rows=1 width=4)
InitPlan
-> Seq Scan on table2 (cost=43.00 rows=1 width=4)
explain SELECT a FROM table1 WHERE table1.b=33 AND
table1.c = ( SELECT d FROM table2 WHERE table2.e=table1.a);
NOTICE: QUERY PLAN:
Seq Scan on table1 (cost=43.00 rows=1 width=4)
SubPlan
-> Seq Scan on table2 (cost=43.00 rows=1 width=4)
You can see that we get an "InitPlan" (ie, run once) for an uncorrelated
subquery but a "SubPlan" (repeat each time) for a correlated one.
Unfortunately, the case you care about is:
explain SELECT a FROM table1 WHERE table1.b=33 AND
table1.c IN ( SELECT d FROM table2 WHERE table2.e=44);
NOTICE: QUERY PLAN:
Seq Scan on table1 (cost=43.00 rows=1 width=4)
SubPlan
-> Seq Scan on table2 (cost=43.00 rows=1 width=4)
The main problem that would have to be solved to convert this to
an InitPlan is what to do if the subselect returns a huge number
of tuples ... with the current implementation, since we scan the
tuples one at a time, there's no problem, but if we try to store
all the tuples we could run out of memory.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Engard Ferenc | 1999-07-08 20:36:31 | offtopic: odbc C frontend for linux? |
Previous Message | Gilson Costa | 1999-07-08 18:41:56 | Referential Integrity |