非常慢的更新声明

时间:2013-08-22 07:28:37

标签: sql database performance select sql-update

我有以下更新语句,它运行了几次并且非常慢(20+秒)。关于如何提高绩效的任何建议?

    update acc_item 
set means_pay_no = m_pay_no
where acc_item_no in (select distinct(acc.acc_item_no) 
                      from acc_item acc, (select accm.acc_item_no acc1,accm.acc_item_no_2 acc2
                                            from acc_item_match accm,acc_payment_item accp
                                            where accp.acc_payment_no=pay_no
                                            and (accm.acc_item_no = accp.acc_item_no or accm.acc_item_no_2 = accp.acc_item_no))
                      where acc.acc_item_no = acc1
                      or acc.acc_item_no = acc2)                    
and reversed_to_acc_item_no is null;

2 个答案:

答案 0 :(得分:1)

如何在临时表中插入语句查询?

select distinct(acc.acc_item_no) into #temptbl 
from acc_item acc, (select accm.acc_item_no acc1,accm.acc_item_no_2 acc2
from acc_item_match accm,acc_payment_item accp
where accp.acc_payment_no=pay_no
and (accm.acc_item_no = accp.acc_item_no or accm.acc_item_no_2 = accp.acc_item_no))
where acc.acc_item_no = acc1
or acc.acc_item_no = acc2


update acc_item 
set means_pay_no = m_pay_no
where acc_item_no in (select * from #temptbl)                    
and reversed_to_acc_item_no is null;

答案 1 :(得分:1)

请改为尝试:

update acc_item 
set means_pay_no = m_pay_no
where exists (select 1
              from acc_item acc 
              where exists 
               (select 1
                from acc_item_match accm,acc_payment_item accp
                where accp.acc_payment_no=pay_no
                and accp.acc_item_no in (accm.acc_item_no, accm.acc_item_no_2)
                and acc.acc_item_no in (accm.acc_item_no, accm.acc_item_no_2)
                ) and acc.acc_item_no = acc_item.acc_item_no
              )
and reversed_to_acc_item_no is null;