declare mon int;
  begin
  for mon in 2..12
  loop
  begin
    update tb1 a set sum(moneys)=(
    select nvl(sum(money1),0) from tb1
    where years=a.years
    and ids=a.ids
    and code = a.code
    and mons<=mon
    and years=2009
    )where mons=mon and years=2009;
  commit;
  end;
  end loop;
  end;
  end;
就这么一个更新语句  其中  要根据ids years code mons 才能锁定一条记录  问题  我现在更新速度超级慢  数据库里一共就70多万条记录  09年的记录  也就不到20万条  如何更新 速度才能快呀 很慢很慢的  受不了了 更新好几个小时了都
     还有个问题    顺便问下  不算重点。。    SQLSERVER里  我可以打印语句 来查看  更新到哪里了 
oracle里呢? 如何查看  ?  dbms_output.put_line(...).这个语句  在没有更新完成之前  无法查看任何东西  有别的方法么 ?

解决方案 »

  1.   

    dbms_output.put_line(...)
    前先要设置 set serveroutput on
      

  2.   

    --你的语句好像是按月汇总update tb1 a set sum(moneys)=( 
        select nvl(sum(money1),0) from tb1 
        where years=a.years 
        and ids=a.ids 
        and code = a.code 
        and trunc(datetime,'MM') = trunc(a.datetime,'MM')   
    );
      

  3.   

    update tb1 a set sum(moneys)... 
    被更新的字段可以用sum()函数?
      

  4.   


    额  这里写错了   就当是MONEYSUM吧。。一个字段 
      

  5.   

    我还以为学到新知识点,可以用
    update tb1 a set sum(moneys)... 
    被更新的字段可以用sum()函数? 原来是错的。
      

  6.   

    你的tb1是不是有很多index和trigger呀?
      

  7.   

    我很奇怪的是  我同样的语句 转化为SQL  很快就更新完了  最多半个小时。。 可是一到ORACLE  好几个小时还没更新出来   这到底是为什么呢?
      

  8.   

    谢谢各位  解决了  我建了个新的表 用insert做的。
    然后把原表的09年数据删掉  再插入回来。。create table 累计额中间表(
    years int,
    code varchar(50),
    mon int,
    money decimal(18,2),
    moneysum decimal(18,2),
    ids int;
    )begin
        
                                      delete from 累计额中间表;
                                      commit;
                                      /**1月**/  
                                      insert into 累计额中间表
                                      select years,code,mon,money,
                                      money,ids from
                                      sum where years=2009 and mon=1;
                                      commit;
                                      /**2月**/
                                      begin
                                      for i in 2..12
                                      loop
                                      begin
                                      insert into 累计额中间表
                                      select a.years,a.code,a.mon,a.money,
                                      sum(b.money),a.ids
                                      from sum a , sum b 
                                      where a.years=2009 and a.mon=i and b.mon<=i
                                      and a.ids=b.ids and a.code=b.code and a.years=b.years
                                      group by a.years,a.code,a.mon,a.money,
                                      a.ids;
                                      commit;
                                      end;
                                      end loop;
                                      end;
                                      end;
    嗯  然后就是把这里面得数据 再插回去  很快  也就不到30秒  30多万条数据就搞定了。。
    希望可以帮到其他人    最后  非常感谢  上面几位的帮助   谢谢。