From: Tom Lane <tgl@sss.pgh.pa.us>
To: Bruce Lowery <bruce.lowery@edventions.com>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] uncorrelated subqueries
Date: Thu, 08 Jul 1999 15:30:06 -0400
Message-ID: <461.931462206@sss.pgh.pa.us> (raw)
In-Reply-To: <37859FE1.D1D95558@edventions.com>
Bruce Lowery <bruce.lowery@edventions.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, bruce.lowery@edventions.com
Subject: Re: [SQL] uncorrelated subqueries
In-Reply-To: <461.931462206@sss.pgh.pa.us>
References: <37859FE1.D1D95558@edventions.com> <461.931462206@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox