Re: IN, EXISTS or ANY?

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: "Josh Berkus" <josh@agliodbs.com>
Cc: pgsql-sql@postgresql.org
Date: 2002-04-29T04:54:23Z
Lists: pgsql-sql
"Josh Berkus" <josh@agliodbs.com> writes:
> I was wondering if there is any difference in execution speed for the
> following three statements:

> WHERE case_id IN (SELECT case_id FROM case_clients
> 		     WHERE matter_no = '123.122342');
> or:

> WHERE case_id = ANY (SELECT case_id FROM case_clients
> 		     WHERE matter_no = '123.122342');
> or

> WHERE EXISTS ( SELECT case_id FROM case_clients
> 		     WHERE matter_no = '123.122342'
> 		     AND case_id = cases.case_id);

IN is the same as = ANY (cf. row_expr production in
src/backend/parser/gram.y for implementation, or SQL92 8.4 rule 4
for specification; there ain't *no* difference).

But EXISTS is an entirely different animal which is often faster
... isn't that in the FAQ?

			regards, tom lane