| |
|
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.
|
 |

07-06-11, 07:24
|
|
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'
|
|

07-06-11, 08:42
|
|
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
|
|

07-06-11, 10:32
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
|
|
Quote:
Originally Posted by Marcus_A
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
|
|

07-06-11, 13:55
|
|
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/
|
|

07-20-11, 09:58
|
|
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.
|
|

07-20-11, 10:01
|
|
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
|
|

07-20-11, 13:08
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
Quote:
Originally Posted by przytula_guy
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 
|
|

07-20-11, 13:38
|
|
:-)
|
|
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
|
|
Quote:
Originally Posted by przytula_guy
this will have a different access plan
|
I'd say "it may have a different access plan, depending on many things".
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|