MYSQL OR vs IN performance

后端 未结 14 847
一向
一向 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:37

    The accepted answer doesn't explain the reason.

    Below are quoted from High Performance MySQL, 3rd Edition.

    In many database servers, IN() is just a synonym for multiple OR clauses, because the two are logically equivalent. Not so in MySQL, which sorts the values in the IN() list and uses a fast binary search to see whether a value is in the list. This is O(Log n) in the size of the list, whereas an equivalent series of OR clauses is O(n) in the size of the list (i.e., much slower for large lists)

提交回复
热议问题