日期:2014-05-17  浏览次数:20894 次

求助:update更新为什么这么慢?
表中per_amount的计算根据该记录以前月份的s_menge减去h_menge的总计,
我用如下的update语句来更新某月份的该字段值,如果数据量大的话速度
非常慢,不知道大家有没有更好的方式?
update   yg_mxledger   t01
      set   per_amount   =   (select   sum(s_menge   -   h_menge)   BB
                                              from   yg_mxledger   t02
                                            where   substr(t02.budat,   1,   6)   <   substr(t01.budat,   1,   6)
                                                and   t01.hkont   =   t02.hkont
                                                and   t01.matnr   =   t02.matnr
and   substr(t02.budat,1,6)   <   '200608 ')
where   substr(t01.budat,1,6)   =   '200608 ';

------解决方案--------------------
update yg_mxledger t01
set per_amount = (select sum(s_menge - h_menge) BB
from yg_mxledger t02
where
t01.hkont = t02.hkont
and t01.matnr = t02.matnr
and substr(t02.budat,1,6) < '200608 ')
where substr(t01.budat,1,6) = '200608 ';

并在substr(yg_mxledger,1,6)上加索引。

------解决方案--------------------
表的索引可能建的有问题,你单独查询速度如何
------解决方案--------------------
性能問題
1 select sum(s_menge - h_menge) BB
from yg_mxledger t02
where substr(t02.budat, 1, 6) < substr(t01.budat, 1, 6)
and t01.hkont = t02.hkont
and t01.matnr = t02.matnr);
這個子查詢需要多少時間?

2
where substr(t01.budat,1,6) = '200608 ';
是否造成全表掃描?
=200608 數據量有多少?

3
per_amount
這個字段上可有索引?
------解决方案--------------------
好象用substr会降低效率.
------解决方案--------------------
where substr(t01.budat,1,6) = '200608 ';
是否造成全表扫描
------解决方案--------------------
1. 猜测yg_mxledger.budat字段类型为VARCHAR2?
where substr(t01.budat,1,6) = '200608 ';
更新条件由于使用了substr函数,导致无法使用索引
yg_mxledger表budat字段上建立索引,并改成like查询调用索引,where t01.budat like '200608% ';
2. 尝试在表yg_mxledger的hkont和matnr 字段上建立索引,希望能够提高更新速度
3. 尽量少在查询表的字段上使用substr等函数,避免查询时耗费太多的时间

update yg_mxledger t01
set per_amount = (select sum(s_menge - h_menge) BB
from yg_mxledger t02
where substr(t02.budat,1,6) < '200608 '
and t01.hkont = t02.hkont
and t01.matnr = t02.matnr )
where t01.budat like '200608% ';
------解决方案--------------------
update
多少条记录?

夺得话,可以分段commit;
------解决方案--------------------
使用where exists替换括号中的where 关键字