背景

公司用户表数据部分字段的部分用户数据不对需要更新,更新的数据在另一个表里。由于之前自己更新出过叉子,所以这次格外小心,没想到还发现了一个美妙的事,不仅更新正确,速度也是很快。我的数据源大概两千多万的样子,抛开数据量说速度也没啥意义,所以我这个有前提的。

解决方法:

方式一:

update test1 a  set a.name = (select b.name from  test2 b where a.id = b.id )
where exists (select b.name from test2 b where a.id = b.id );

方式二: (如果返回多个数据有重复需要去重)

merge into test1 a
using  test2 b on (a.id = b.id)
when matched then 
update set a.name = b.name;

方式二的速度比方式一的快很多。