我确实需要知道这一点,因此我对这两种方法进行了基准测试。我始终发现
IN它比使用快得多
OR。
不要相信给出意见的人,科学就是测试和证据。
我运行了1000倍等效查询的循环(出于一致性考虑,我使用
sql_no_cache):
IN:2.34969592094s
OR:5.83781504631s
更新:(
我没有原始测试的源代码,就像6年前一样,尽管它返回的结果与此测试范围相同)
在要求一些示例代码进行测试时,这是最简单的用例。使用Eloquent简化语法,等效的原始SQL执行相同的操作。
$t = microtime(true); for($i=0; $i<10000; $i++):$q = DB::table('users')->where('id',1) ->orWhere('id',2) ->orWhere('id',3) ->orWhere('id',4) ->orWhere('id',5) ->orWhere('id',6) ->orWhere('id',7) ->orWhere('id',8) ->orWhere('id',9) ->orWhere('id',10) ->orWhere('id',11) ->orWhere('id',12) ->orWhere('id',13) ->orWhere('id',14) ->orWhere('id',15) ->orWhere('id',16) ->orWhere('id',17) ->orWhere('id',18) ->orWhere('id',19) ->orWhere('id',20)->get();endfor;$t2 = microtime(true); echo $t."n".$t2."n".($t2-$t)."n";1482080514.3635
1482080517.3713
3.0078368186951
$t = microtime(true); for($i=0; $i<10000; $i++): $q = DB::table('users')->whereIn('id',[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])->get(); endfor; $t2 = microtime(true); echo $t."n".$t2."n".($t2-$t)."n";1482080534.0185
1482080536.178
2.1595389842987



