drop table total;
create table total as select empid,sum(nvl(money,0)) money from detail group by empid;

解决方案 »

  1.   

    楼上的兄弟,实际上我的detail  和 total 表里有很多字段 ,这么做不行。
    这2个表是很复杂的表的
      

  2.   

    update total a set 
     money=(select sum(nvl(money,0))
      from detail  where a.empid= empid group by 
    empid  ) 改为update total a set 
     money=(select sum(nvl(money,0))
      from detail where a.empid= empid )1. 去除group by empid
    2. 查看一下detail对empid是否有索引。
      

  3.   

    update total a set 
                   money=(select sum(nvl(b.money,0))
                   from detail b where a.empid=b.empid )
    where exists (select 1 from detail b where a.empid=b.empid );
      

  4.   

    update total 
       set money=(select sum(nvl(detail.money,0)) 
                     from detail where total.empid=detail.empid)
       where total.empid=detail.empid
      

  5.   

    多写了一行,sorry。
    update total 
       set money=(select sum(nvl(detail.money,0)) 
                     from detail where total.empid=detail.empid)
      

  6.   

    jxc(GameHeart) : 是全表扫描,但detail不建索引的话,对total的每一条记录都会在detail上进行全表扫描
      

  7.   

    好像和楼主写的一样,楼主把我写的删了吧,so sorry.
      

  8.   

    非得用一个语句解决么?
    偶觉得
    update total a 
    set   money=(select sum(nvl(money,0)) from detail where a.empid= empid )
    where exists (select 1 from detail b where a.empid=b.empid )
    效率应该是比较高的--
    当然前提是两个表的index!
    不过偶一般采用PL/SQL来写:
    /***
    一个DETAIL 表,
    item ,empid, money
    1       001    100
    2       001     150 
    ...    ...     ...一个 TOTAL 表,
    empid ,  money
    **/
    declare
        cursor the_cur is 
        select empid, sum(nvl(money,0)) money
        from detail
        group by empid
        order by empid;
    begin 
        for my_cur in the_cur loop
           update total
           set    money=my_cur.money
           where  empid=my_cur.empid;
        end loop;      
    end;个人测试表明:这样写似乎比update效率高许多~~~
      

  9.   

    呵呵。。问题搞定了,让各位费心了,
    原来我的 detail 表没有设置关键字,
    关键字设置后, update 只需要 2s了。。
      

  10.   

    考虑了这么多的情况,原来仅仅只是要设置一个关键字就可以了。。
     汗 !!因为我发现这种类似的语句在其他地方都很快的,就这里慢,才发现是detail 表没有关键字
      

  11.   

    index 不是好几个人提了么?
    只是措词不一样而已!