1、前提是必须有相同的项目,如id主键。 使用sql server 2008 中的新增语句merge2、若不满足1,则只能通过逐行比较了
1。有顺序是否是有默认的规则,如根据规则,不有查100万的表即可知1万的表要插入那些行。 如果是,就根据规则过滤1万行数据再插入 2。先试试 select * from [1万表] as a where not exists(select * from 大表 where 主键=a.主键) 的查询速度,时间不是很入就用一个语句插入
insert into tb1 select * from tb2 except select * from tb1
使用临时表,a代表1万,b代表百万,匹配字段为id,#in 代表有的那部分,#out代表没有的部分select a.* into #in from a inner join b on a.id=b.idselect a.* into #out from a left outer join #in on a.id=b.id where b.id is nullupdate b set b.id=#in.id,b.c1=#in.c1.... from b inner join #in on #in.id=b.id insert into b select * from #out drop table #in drop table #out
使用sql server 2008 中的新增语句merge2、若不满足1,则只能通过逐行比较了
如果是,就根据规则过滤1万行数据再插入
2。先试试
select * from [1万表] as a where not exists(select * from 大表 where 主键=a.主键)
的查询速度,时间不是很入就用一个语句插入
select * from tb2
except
select * from tb1
insert into b select * from #out
drop table #in
drop table #out