mysql where id = 与 where id IN() 之间的性能差异

mysql performance difference between where id = vs where id IN()

我对 mysql 很陌生。我有一个查询,从这个查询中我得到一些 stock_ids.In 第二个查询我需要传递那些 stock_ids。为此,我正在获取 array.Now 中的所有 stock_ids 我的问题是我可以通过哪种方式将这些 stock_ids 传递给第二个查询。为此,我有两种方法。

First approach:
$array_cnt = count($stockid_array);
for($i = 0; $i<$array_cnt;$i++)
{
$sql = "select reciever_id,sender_identifier,unique_stock_id,vat_number,tax_number from stocktable where stock_id = '".$stockid_array[$i]."'";
// my while loop
}
Another approach is 

$sql = "reciever_id,sender_identifier,unique_stock_id,vat_number,tax_number from stocktable where stock_id in ('1','2','3','4','5','6','7','8','9');
//while loop comes here.

那么哪种方法性能好?请指导我。

MySQL 对 in 列表中的常量进行了很好的优化——它基本上对值进行排序并使用二进制搜索。这意味着 in 会更快。此外,in 可以利用列上的索引。

此外,运行 单个查询应该比 运行 多个查询更快。

因此,in 版本应该优于 运行 多个 = 查询。