两种说法的区别

问题描述

声明 1:

delete from table_a where <condition>

声明 2:

delete from table_a where id in (select id from table_a <condition>)

这意味着,<condition> 将选择出 50 万个元组

这些语句会导致不同的锁吗?第二个会更安全吗(死锁的可能性更小)?

解决方法

delete from table_a where <condition>
delete from table_a where id in (select id from table_a <condition>)

显然第一个更好。 因为在第二个你重复同样的事情,同时添加更多无用的请求。 在我看来它并不好看,而且它消耗了太多时间。

如果这两个表不同,那就可以理解了。 示例:table_atable_b

delete from table_a where a_id in (select b_id from table_b <condition>)
,

@Sega 是正确的,只要 ID 是唯一的。但是,如果 ID 不是唯一的,那么结果很可能会大不相同。

  • 第一条语句表示删除满足给定条件的所有行。
  • 第二个说删除至少有 1 行的 ID 的所有行 匹配给定的条件。

demo