If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Using equals or IN

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-11, 07:24
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Using equals or IN

Hi,
Can anyway tell me is it better to do:

SELECT Lastname, workdept
FROM Employee
WHERE workdept IN ('E11', 'E21')

and

SELECT Lastname, workdept
FROM Employee
WHERE workdept = 'E11' OR workdept = 'E12'
Reply With Quote
  #2 (permalink)  
Old 07-06-11, 08:42
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Do an Explain and find out.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 07-06-11, 10:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Marcus_A View Post
Do an Explain and find out.
and this will tell you whether there is a performance difference

in addition to that consideration, there is the question of query clarity, comprehension, and maintainability -- for these, the IN syntax is ~much~ preferred
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-06-11, 13:55
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Let's say that (in your example) "IN" will never be less performant than "OR", for any RDBMS and version.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #5 (permalink)  
Old 07-20-11, 09:58
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Hi,
Just ran two similar queries to above...

1.
SELECT EMPID, NAME, DEPT FROM EMPLOYEES WHERE EMPID=0;

AND

2.
SELECT EMPID, NAME, DEPT FROM EMPLOYEES WHERE EMPID IN(0);

This table is pretty simple has no indexes and very little data.
Exact same explain / access plan for both queries. Even the number of timerons where the same.

Then I change EMPID to be an index. Again exact same explain plan for both.

I am just putting this information so anyone who is reading this thread gets the conclusion that different syntax does not mean different explain plan.

Cheers.
Reply With Quote
  #6 (permalink)  
Old 07-20-11, 10:01
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
in the first example you stated
WHERE workdept IN ('E11', 'E21')
next sample
WHERE workdept = 'E11' OR workdept = 'E12'
this will have a different access plan

this is not the same as single values without OR
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #7 (permalink)  
Old 07-20-11, 13:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by przytula_guy View Post
in the first example you stated
WHERE workdept IN ('E11', 'E21')
next sample
WHERE workdept = 'E11' OR workdept = 'E12'
this will have a different access plan
wow, if that's true, then DB2 is a totally weird database system

all the other database systems i've worked with will use the exact same plan for them

of course, because of the values, they will produce different results, but that's a different issue
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 07-20-11, 13:38
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by przytula_guy View Post
this will have a different access plan
I'd say "it may have a different access plan, depending on many things".
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On