有两条语句,一条是
Merge into a using (select …… from b where ……) on a.id=b.id
 when matched then update …… 
 when not matched then insert ……一条是
truncate table a;
insert into a select …… from b where ……;
哪条更优呢?效率更高?

解决方案 »

  1.   

    A:Merge into a using (select …… from b where ……) on a.id=b.id
     when matched then update …… 
     when not matched then insert ……B:
    truncate table a;
    insert into a select …… from b where ……;A 是这样一个过程,逐行判断b表的数据,如果在a表中有满足条件的就更新,否则就添加
    B 是这样一个过程,把b表中的数据添加到a表中这是功能完全不同的两个语句,不能比较效率问题。当然,如果你非要考虑效率问题,那么如果using子句中的b表是个大表,则Merge 的效率会非常低,因为merge是一条一条判断的。