求助:
场景:需要将A表的数据同步到B表,数据量为50万条。
解决方案:
1.读取A表中所有数据,在程序中循环,使用存储过程比对B表中数据,如不存在则更新,如果不存在则添加
2.读取A表和B表中所有数据,使用程序判断是否存在,然后再更新到B表请问哪一种方法效率更高,如果有更好的方法,请指教
场景:需要将A表的数据同步到B表,数据量为50万条。
解决方案:
1.读取A表中所有数据,在程序中循环,使用存储过程比对B表中数据,如不存在则更新,如果不存在则添加
2.读取A表和B表中所有数据,使用程序判断是否存在,然后再更新到B表请问哪一种方法效率更高,如果有更好的方法,请指教
个人觉得这样不好。你可以先查出A表中有且B表中没有的数据,然后再insert 到B表,直接在数据库端操作。insert B表
select * from A表 a where noe exists(select 1 from B表 b where a.col=b.col)
及时是更新和删除,比对的次数也远远小于你的设计方案。如果有更好的方案欢迎来拍!
1.update
update b set ... from b , a where b.关键字 = a.关键字2.insert
insert into b(需要的字段一一列出) select 需要的字段按照B表的顺序和类型一一列出 from a where not exists(select 1 from b where b.关键字 = a.关键字)如果B,A两表字段类型和顺序一样,则可简写为如下:
insert into b select * from a where not exists(select 1 from b where b.关键字 = a.关键字)