问题
if i have a list of Ids (1, 4,6,7) and a db table that i want to delete all records where id is in this list, what is the fastest way of doing this?
回答1:
Your question almost spells the SQL for this:
DELETE FROM table WHERE id IN (1, 4, 6, 7)
回答2:
delete from t
where id in (1, 4, 6, 7)
回答3:
The correct answer is that speed depends on the characteristics of the particular implementation/engine that you are using, and that you should beware of any quack who pretends to be able to give a definite answer to this question.
If the optimizer of your system is so poor that it does not spot the optimization opportunity from 'WHERE id IN (...)', then it will do a table scan, which might be a lot slower than giving 4 separate delete commands.
来源:https://stackoverflow.com/questions/2615566/delete-where-id-in-list