I’m using MySQL. Tables engine is MyIsam
Which method is faster?
DELETE FROM TABLE WHERE id IN (1,2,3);
or
DELETE FROM TABLE WHERE id = 1;
DELETE FROM TABLE WHERE id = 2;
DELETE FROM TABLE WHERE id = 3;
id field is the PRIMARY KEY
Which one will work faster?
IN should be faster, because mysql will update your indexes and move data blocks after every query in your second solution, while it will happen only once with the first query.
And here are some tests on MySQL (for a table having one int column and 3 varchars, populated with random data and on index on a column in the WHERE…makes no sense without index, because it takes a lot more time in both cases…but still a loooot slower with 3 queries than IN).
So, those three queries, took a lot more time 7.39s vs 5.25s for IN, which is a 40% increase). Here is prepare_data procedure: