I have to delete records that contain the id in an array.
I just don’t know which is better to use. Which is faster and more efficient?
code 1
$array = array('123','456','789' ...)//over 900 entries
$id = implode(',', $array);
$sql = 'DELETE FROM email WHERE id IN ('.$id.')';
//execute sql
or
code2
$array = array('123','456','789' ...)//over 900 entries
for($x=0;$x<count($array);$x++){
$sql='DELETE FROM email WHERE id = '.$array[$x].' ';
//execute sql
}
Which from the two is faster and more efficient when executed?
Case 1 will be faster.
It is not only executing time of the query that determines the total amount of time. Parsing, preparing and planning also take time. In case 2 that needs to be done several times, in case 1 only once. Deleting a row will be equally fast, but it is the query overhead that makes case 1 faster.