今天在写存储过程的时候碰到了一个问题,希望大神们给我看看,
由于是银行搞开发,源代码没法拿下来,我在这里基本简单写一下:
首先:我建立两个集合
type loan_card_no_type is table of ma_loan_card.loan_card_no%type
index by binary_integer;
type loan_date_type is table of varchar2(10)
index by binary_integer;my_loan_card_no loan_card_no_type;
my_loan_date loan_date_type;然后对数据进行批量绑定select:
select a.loan_card_no,to_char((b.loan_date-1),'YYYY-MM-DD') bulk collect
into my_loan_card_no,my_loan_date
from ma_loan_card where ....;
--大致就是这样,字段是这么取的,用时差不多20秒左右,表中有50万条数据
批量绑定以后,my_loan_card_no 和ma_loan_date中差不多有9万条记录:问题主要在这里,在update的时候巨慢:
forall i in 1..my_loan_card_no.count
update acc_loan_card set beg_date=my_loan_date(i),valid_flag='1'
where loan_card_no=my_loan_card_no(i) and valid_flag='0';
--acc_loan_card 这个表中也就50万条记录左右
然后在跑存储过程跑了四五十分钟还没跑完,平时用游标一条条处理这个时间都能出来了,我郁闷了。希望大神给我解惑。非常感谢!

解决方案 »

  1.   

    不要用游标,游标太慢了select a.loan_card_no,to_char((b.loan_date-1),'YYYY-MM-DD') bulk collect
    into my_loan_card_no,my_loan_date
    from ma_loan_card where ....;update acc_loan_card set beg_date=my_loan_date(i),valid_flag='1'
    where loan_card_no=my_loan_card_no(i) and valid_flag='0';可以合成一条语句执行更新
      

  2.   

    UPDATE  (SELECT 
    from a.loan_card_no,a.beg_date,a.valid_flag,to_char((b.loan_date-1),'YYYY-MM-DD') as set_date
    from ma_loan_card a
    where ...
    ) t
        SET   beg_date=set_date,valid_flag='1'
    必要时加:/*+bypass_ujvc+*/