IN, EXISTS or ANY?

Поиск
Список
Период
Сортировка
Искать

IN, EXISTS or ANY?

От:
"Josh Berkus" <josh@agliodbs.com>
Дата:
Folks,

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);

... or does the parser handle all three exactly the same way?

-Josh Berkus


Re: IN, EXISTS or ANY?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Josh Berkus"  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

Re: IN, EXISTS or ANY?

От:
"Josh Berkus" <josh@agliodbs.com>
Дата:
Tom,

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

OK, still true, then hey?  So for subqueries where I expect a
medium-to-large result set, I should use EXISTS.  Got it.

BTW, just tried the new CURSOR implementation for PL/pgSQL for the
first time.  Nice work, guys!

-Josh

Re: IN, EXISTS or ANY?

От:
Keith Gray <keith@heart.com.au>
Дата:
Josh Berkus wrote:

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


There is no reference to EXISTS in the SELECT documentation??

Is this explained somewhere else?



-- 

Keith Gray
Technical Services Manager
Heart Consulting Services


Re: IN, EXISTS or ANY?

От:
"Josh Berkus" <josh@agliodbs.com>
Дата:
Keith,

> >>But EXISTS is an entirely different animal which is often faster
> >>... isn't that in the FAQ?
> 
> 
> There is no reference to EXISTS in the SELECT documentation??
> 
> Is this explained somewhere else?

Hmmm ....  found this using the Index for the online docs:

http://www.us.postgresql.org/users-lounge/docs/7.2/postgres/functions-subquery.html

Have fun.

-Josh


FAQ