2表(多表)update 2个(多个)值 sql怎么写?其中table1表100多万条数据,table2表30多万条,最多也就是更新table1中的30多万条,其余的不更新
更新1列,可以按下面的写,如果要更新多个值,sql怎么写?update table1 a
   set a.col3 = nvl((select b.col3
                             from table2 b
                            where b.col1 = a.col1
                              and b.col2 = a.col2), a.col3);update操作怎么优化,能提高执行效率?
谢谢!

解决方案 »

  1.   

    楼主这样是全表更新
    这样会快点
    update table1 a
    set a.col3 = (select b.col3
    from table2 b
    where b.col1 = a.col1)
    where exists (
    select 1
    from table2 b
    where b.col1 = a.col1)
      

  2.   

    要么写个存储过程不知道能不能快点declare
     cursor c_name is select col1, col3 from table2;
    begin
       for tab in c_name loop
          update table1
             set col3=tab.col3
          where col1.tab.col1;
      end loop;
    end;
      

  3.   

    试过了,很慢,测试了一下,更新5000条数据要20多分钟
    被更新的表table1有几个index,不知道是不是跟index有关呢?
      

  4.   

    如果更新的字段中含有INDEX,最好先DROP掉,更新完了再重建.
      

  5.   

    update table1 a
       set a.col3 = (select b.col3
                                 from table2 b
                                where b.col1 = a.col1
                                  and b.col2 = a.col2 and b.col3 is not null );table2的col1,col2,col3加index
    table1的index先drop掉,之后重建
      

  6.   

    同意楼上的,更新表,如果数据比较多最好把index先drop掉,会快一点。
      

  7.   

    表数据量比较多的话,进行关联操作一定要建索引.
    update table1 a
    set (a.col3,a.col4)=(select b.col3,b.col4 
                         from table2 b
                         where b.col1=a.col1
                           and b.col2=a.col2)
    像上面的语句,col1,col2都要建索引.