对于一个表ktr_work的update更新语句有20多条, 这些更新语句全部都是从kmt_dakmx表中提取对应数据来更新ktr_work表中的对应栏位,并且需要按先后顺序更新来进行一些判断, 我对where后面的条件都建了对应的索引,但时每次执行这样的更新都要花费20多分钟时间,并且CPU的占用会超过90%,电脑好象死机一样。 在企业管理器中查看"锁/进程ID",发现spid54进程中对于ktr_work表的锁有500多个,请问如何优化更新语句? CREATE PROCEDURE [kq_shxb_date_qr] @lc_gongh31 char(20), @lc_yue31 char(6), @lc_date31 datetime, @lc_date32 datetime AS begin -------------------------------------------------------------------------------------------------------- update a set shw_shb_sh = '', shw_xb_sh = '',... from ktr_work a where a.yue = @lc_yue31 and .... -------------------------------------------------------------------------------------------------------- update d set shw_shb_sh = c.date_t from ktr_work d left outer join (select top 1000000000 a.yue, a.id_gongh, a.date_d, min(isnull(b.date_t,'')) as date_t from ktr_work a left outer join kmt_dakmx b on A.id_gongh = B.id_gongh and (a.date_d=b.date_d or a.date_d=dateadd(day,-1,b.date_d)) where a.yue = @lc_yue31 and .... group by a.yue, a.id_gongh, a.date_d order by a.yue, a.id_gongh, a.date_d) c on d.yue=c.yue and d.id_gongh=c.id_gongh and d.date_d=c.date_d where c.id_gongh is not null --------------------------------------------------- update c set shw_xb_sh = isnull(d.date_t,'') from ktr_work c left outer join (select top 1000000000 a.yue, a.id_gongh, a.date_d, min(b.date_t) as date_t from ktr_work a left outer join kmt_dakmx b on A.id_gongh = B.id_gongh and (a.date_d=b.date_d or a.date_d=dateadd(day,-1,b.date_d)) where a.yue = @lc_yue31 and ... group by a.yue, a.id_gongh, a.date_d order by a.yue, a.id_gongh, a.date_d) d on (c.yue=d.yue and c.id_gongh = d.id_gongh and c.date_d = d.date_d) where c.yue = @lc_yue31 and ..... --------------------------------------------------- update c set xw_shb_sh = isnull(d.date_t,'') from ktr_work c where .... --------------------------------------------------- --对于ktr_work表的更新语句大约有20多条. --..... end
这个问题第1个回答:
把左外联改成内联试试
这个问题第2个回答:
它这些update语句拆成几个独立的存储过程,单独执行这些存储过程,看执行速度是否还会慢,是否还会形成锁.
|