Re: JOIN vs. WHERE ... IN (subselect) - Mailing list pgsql-sql

From Stephan Szabo
Subject Re: JOIN vs. WHERE ... IN (subselect)
Date
Msg-id 20030517090929.M99020-100000@megazone23.bigpanda.com
Whole thread Raw
In response to JOIN vs. WHERE ... IN (subselect)  ("Dean Gibson (DB Administrator)" <dba-sql@ultimeth.net>)
List pgsql-sql
On Fri, 16 May 2003, Dean Gibson (DB Administrator) wrote:

> Using PostgreSQL 7.3.2 on Linux.
>
> One of the types of queries that I like to do on our database is:
>
> SELECT * FROM table1
>     WHERE indexed_column1 IN
>     (SELECT column2 FROM table2 WHERE <condition>);
>
> However, my third-party SQL book says that the above is equivalent to:
>
> SELECT table1.* FROM table1, table2
>     WHERE indexed_column1 = column2 AND <condition>;
>
> And indeed, the above query executes virtually instantaneously if
> "<condition>" results in a small number of rows from table2.
>
> I'm fairly new to SQL;  are they really equivalent?  If so, is there
> some reason that the first form is not optimized/transformed into the
> second form, or is this a planned enhancement?

7.4 already handles IN differently than previous versions and should
runs queries like the first one better than current versions.

And those two queries are not equivalent in any case unless the subselect
is guaranteed to only have one matching row (or you use a distinct in the
second query).



pgsql-sql by date:

Previous
From: "Dean Gibson (DB Administrator)"
Date:
Subject: Re: JOIN vs. WHERE ... IN (subselect)
Next
From: "Terry Yapt"
Date:
Subject: COALESCE in ORDER BY...