Use IN or multiple ORs? Which one has the best performance?

4

I have the following queries in MySQL:

1st: Using multiple OR

 SELECT SUM(qtd)
    FROM 
        produtos
    WHERE 
        qtd > 10 
        and (status = '0' or status = '4' or status = '7')

2nd: Using IN

 SELECT SUM(qtd)
    FROM 
        produtos
    WHERE 
        qtd > 10 
        and status IN (0,4,7)

Which query will perform best? Will both of these be the same data or difference?

    
asked by anonymous 06.07.2017 / 15:45

1 answer

4

IN is an internal implementation and can be optimized better. It should not have big gain, but it's to be faster for a large comparison list.

But in general the choice must be in terms of semantics, not performance. The IN looks for a value in a data set, the OR concatenates conditions and is only worth the use if it is a small amount. For small amounts the performance will not make any difference. For large comparison volume OR is virtually unfeasible.

IN is more powerful to be able to buy with a set of data to be determined.

When it does not matter what to use, choose a shape and always be consistent.

    
06.07.2017 / 16:19