(1) insert [into] b select id, field1, field2 from a where not exists (select id from b where id=[a.]id)
(2) insert [into] b select * from a where id not in (select id from b)
---------------------------------------------------------------------------
这两句,都是将 a 表中存在, 但b表中不存在的数据, 插入到b表中, 关联比较字段为 id.
但这两句的执行效率,却是有数量级的差别.
结论有两点:
- 尽量用 exists 和 not exists 代替 in 和 not in
- 不要偷懒, 尽量不用 select * from ...., 而要写字段名 select field1,field2,....