Connecting Tech Pros Worldwide Help | Site Map

Difference between IN and OR

madhoriya22's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: India
Posts: 254
#1: Aug 29 '07
HI,
I am using this query to get the count from table
Expand|Select|Wrap|Line Numbers
  1.  
  2. select count(*) from defect_detail where status = 'RESOLVED' OR 'CLOSED' OR 'VERIFIED'
  3.  
It is retruning count 15. And when I am using this query
Expand|Select|Wrap|Line Numbers
  1.  
  2. select count(*) from defect_detail where status in ( 'RESOLVED' , 'CLOSED' , 'VERIFIED')
  3.  
Then it is returnin count as 19. Which one is the correct?
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Aug 29 '07

re: Difference between IN and OR


Heya, madhoriya.

This is more correct than either of the two:
Expand|Select|Wrap|Line Numbers
  1. select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
  2.  
Note that the items in the IN subclause are sorted. This way, MySQL can use a binary search on the options, which is much faster than going through the list sequentially.

True, MySQL will automatically sort the items for you, but why make it do all that extra work when the values are constants?

Admittedly, when you only have three options, the time savings isn't great, but it's a good habit to get into.

The problem with this statement, which I will go ahead and 'explode' so you can get a better picture of what's going on:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             'CLOSED'
  10.         OR
  11.             'VERIFIED'
  12.     )
  13.  
Note that MySQL will only check to see if `status` = 'RESOLVED'. When you use "OR 'CLOSED'", MySQL evaluates 'CLOSED' as true, so in essence your query is doing this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             true
  10.         OR
  11.             true
  12.     )
  13.  
which, of course actually counts ALL the records in the table.

Now, you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             `status` = 'CLOSED'
  10.         OR
  11.             `status` = 'VERIFIED'
  12.     )
  13.  
but it's MUCH faster, especially as your table grows in size, to use IN instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.         `status`
  7.             IN
  8.             (
  9.                 'CLOSED',
  10.                 'RESOLVED',
  11.                 'VERIFIED'
  12.             )
  13.  
madhoriya22's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: India
Posts: 254
#3: Aug 29 '07

re: Difference between IN and OR


Quote:

Originally Posted by pbmods

Heya, madhoriya.

This is more correct than either of the two:

Expand|Select|Wrap|Line Numbers
  1. select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
  2.  
Note that the items in the IN subclause are sorted. This way, MySQL can use a binary search on the options, which is much faster than going through the list sequentially.

True, MySQL will automatically sort the items for you, but why make it do all that extra work when the values are constants?

Admittedly, when you only have three options, the time savings isn't great, but it's a good habit to get into.

The problem with this statement, which I will go ahead and 'explode' so you can get a better picture of what's going on:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. 'CLOSED'
  10. OR
  11. 'VERIFIED'
  12. )
  13.  
Note that MySQL will only check to see if `status` = 'RESOLVED'. When you use "OR 'CLOSED'", MySQL evaluates 'CLOSED' as true, so in essence your query is doing this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. true
  10. OR
  11. true
  12. )
  13.  
which, of course actually counts ALL the records in the table.

Now, you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. `status` = 'CLOSED'
  10. OR
  11. `status` = 'VERIFIED'
  12. )
  13.  
but it's MUCH faster, especially as your table grows in size, to use IN instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. `status`
  7. IN
  8. (
  9. 'CLOSED',
  10. 'RESOLVED',
  11. 'VERIFIED'
  12. )
  13.  

Hi,
Thanks buddy. Nice description ! :)
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Aug 29 '07

re: Difference between IN and OR


Heya, Madhoriya.

We aim to please.

Good luck with your project, and if you ever need anything, post back anytime :)
Reply