Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2007, 07:55 PM
aj
Guest
 
Posts: n/a
Default IN versus ANY versus EXISTS

DB2 8.2 LUW FP14

Is there any real difference between
select blahblahblah... where blah IN (select blah......)
versus
select blahblahblah... where blah = ANY (select blah.....)
versus
select blahblahblah... where exists (select blah.....)

Is one more ANSI than another? Which is most portable?

TIA

aj
  #2  
Old July 24th, 2007, 08:55 PM
David Portas
Guest
 
Posts: n/a
Default Re: IN versus ANY versus EXISTS

"aj" <ronald@mcdonalds.comwrote in message
news:13a9u74g89euodc@news.supernews.com...
Quote:
DB2 8.2 LUW FP14
>
Is there any real difference between
select blahblahblah... where blah IN (select blah......)
versus
select blahblahblah... where blah = ANY (select blah.....)
versus
select blahblahblah... where exists (select blah.....)
>
Is one more ANSI than another? Which is most portable?
>
TIA
>
aj
IN, ANY and EXISTS are all ANSI SQL 92.

A IN (B)

is equivalent to

A = ANY (B)

In fact the SQL standard defines IN by using the ANY syntax.

EXISTS is entirely different. Probably you meant to imply some correlated
subquery in place of some simple subqueries in your other examples. Without
seeing the actual queries, DDL etc I won't speculate on whether such queries
might be equivalent.

--
David Portas




  #3  
Old July 25th, 2007, 02:05 PM
Brian Tkatch
Guest
 
Posts: n/a
Default Re: IN versus ANY versus EXISTS

On Mon, 23 Jul 2007 14:51:49 -0400, aj <ronald@mcdonalds.comwrote:
Quote:
>DB2 8.2 LUW FP14
>
>Is there any real difference between
select blahblahblah... where blah IN (select blah......)
>versus
select blahblahblah... where blah = ANY (select blah.....)
>versus
select blahblahblah... where exists (select blah.....)
>
>Is one more ANSI than another? Which is most portable?
>
>TIA
>
>aj
Yes. IN means the compiler needs to put together a result set, and see
if it matches. EXISTS wll use any availible INDEX.

For smaller reult sets IN() is usually faster, for larger use EXISTS.
In some cases execution time will be the same, in others, there may be
a significant difference.

B.
  #4  
Old July 25th, 2007, 07:25 PM
Knut Stolze
Guest
 
Posts: n/a
Default Re: IN versus ANY versus EXISTS

Brian Tkatch <N/Awrote:
Quote:
On Mon, 23 Jul 2007 14:51:49 -0400, aj <ronald@mcdonalds.comwrote:
>
Quote:
>>DB2 8.2 LUW FP14
>>
>>Is there any real difference between
> select blahblahblah... where blah IN (select blah......)
>>versus
> select blahblahblah... where blah = ANY (select blah.....)
>>versus
> select blahblahblah... where exists (select blah.....)
>>
>>Is one more ANSI than another? Which is most portable?
>>
>>TIA
>>
>>aj
>
Yes. IN means the compiler needs to put together a result set, and see
if it matches. EXISTS wll use any availible INDEX.
I wouldn't draw such a conclusion. The optimizer could rewrite one form
into the other and, thus, result in the same access plan.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
  #5  
Old July 25th, 2007, 07:55 PM
Brian Tkatch
Guest
 
Posts: n/a
Default Re: IN versus ANY versus EXISTS

On Wed, 25 Jul 2007 20:18:30 +0200, Knut Stolze <stolze@de.ibm.com>
wrote:
Quote:
>Brian Tkatch <N/Awrote:
>
Quote:
>On Mon, 23 Jul 2007 14:51:49 -0400, aj <ronald@mcdonalds.comwrote:
>>
Quote:
>>>DB2 8.2 LUW FP14
>>>
>>>Is there any real difference between
>> select blahblahblah... where blah IN (select blah......)
>>>versus
>> select blahblahblah... where blah = ANY (select blah.....)
>>>versus
>> select blahblahblah... where exists (select blah.....)
>>>
>>>Is one more ANSI than another? Which is most portable?
>>>
>>>TIA
>>>
>>>aj
>>
>Yes. IN means the compiler needs to put together a result set, and see
>if it matches. EXISTS wll use any availible INDEX.
>
>I wouldn't draw such a conclusion. The optimizer could rewrite one form
>into the other and, thus, result in the same access plan.
You know, the optimizer really gets in the way here. :P

B.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles