SQL查询语句优化的一点

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 alexzhang00 的 blog

(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.
但这两句的执行效率,却是有数量级的差别.

结论有两点:

  1. 尽量用 exists 和 not exists 代替 in 和 not in
  2. 不要偷懒, 尽量不用 select * from ...., 而要写字段名 select field1,field2,.... 

本文关键:SQL查询语句优化的一点
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top