SQL 删除列表中的 where id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2615566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete where id in list
提问by leora
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?
如果我有一个 Id 列表(1、4、6、7)和一个 db 表,我想删除该列表中 id 的所有记录,那么最快的方法是什么?
回答by Matti Virkkunen
Your question almost spells the SQL for this:
您的问题几乎拼写了 SQL:
DELETE FROM table WHERE id IN (1, 4, 6, 7)
回答by Carl Manaster
delete from t
where id in (1, 4, 6, 7)
回答by Erwin Smout
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.
如果您系统的优化器太差以至于它没有从“WHERE id IN (...)”中发现优化机会,那么它会进行表扫描,这可能比提供 4 个单独的删除命令要慢得多。