日期:2014-05-18  浏览次数:20570 次

请教一条SQL 写法
就是将a表有的数据但b表没有的数据插入到b表
insert   into   b   select   *   from   a   where   id   not   in(select   id   from   b)  
请问   这在SQL     上有更好的优化吗?

在线等     急~~!!!!

------解决方案--------------------
試試這個

insert into b select A.* from a left Join b On a.id = b.id Where b.id is null
------解决方案--------------------
insert into b
select * from a
where not exists(select 1 from b where b.id=a.id)

在b.id上建索引
------解决方案--------------------
insert into b
select * from a as tmp
where not exists(select 1 from b where id=tmp.id)

------解决方案--------------------
同意paoluo(一天到晚游泳的鱼)的想法,这个通过左连接的一般比not in与not exists效率高,但是如果想下率更高,可以在gahade(与君共勉) 所有的方式建立索引,两个表的id都建立索引,然后用paoluo(一天到晚游泳的鱼)的:insert into b select A.* from a left Join b On a.id = b.id Where b.id is null

------解决方案--------------------
左连接的一般比not in与not exists效率高??
------解决方案--------------------
insert into b select a.* from a where a.id not in(select distinct b.id from b)
------解决方案--------------------
左连接的一般比not in与not exists效率高??

我不同意这种说法,exists 通常比 in / like 更快.
最好用exists 再在ID上建索引
------解决方案--------------------
同意free_pop2k() ( )的说法