MYSQL OR vs IN performance

后端 未结 14 1097
一向
一向 2020-11-22 04:12

I am wondering if there is any difference in regards to performance between the following

SELECT ... FROM ... WHERE someFIELD IN(1,2,3,4)

SELECT ... FROM ..         


        
14条回答
  •  星月不相逢
    2020-11-22 04:41

    I'll bet they are the same, you can run a test by doing the following:

    loop over the "in (1,2,3,4)" 500 times and see how long it takes. loop over the "=1 or =2 or=3..." version 500 times and seeing how long it runs.

    you could also try a join way, if someField is an index and your table is big it could be faster...

    SELECT ... 
        FROM ... 
            INNER JOIN (SELECT 1 as newField UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) dt ON someFIELD =newField
    

    I tried the join method above on my SQL Server and it is nearly the same as the in (1,2,3,4), and they both result in a clustered index seek. I'm not sure how MySQL will handle them.

提交回复
热议问题