2014-09-11 77 views
0

我是一个非数据库家伙..你可以请你分享你的意见,以改善下面的sp样本,如使用全局临时表或索引或可能会改善现有的查询。在原代码中,我会对不同的桌子有很多更新查询。谢谢!提高存储过程性能

CREATE OR REPLACE PROCEDURE SYSTEM.process_log1 
IS 
    cursor cur_attuid_change is 
select 
    sup.id as id, 
    sup.name as name, 
    sup.role as role, 
    sup.technology as technology 
from main hr, secondary sup 
where sup.id=hr.id; 

BEGIN 
    -- update records in main tables from the cursor 
    for rec_attuid_change in cur_attuid_change loop 

    update main t1 
    set t1.id = rec_attuid_change.id, 
      t1.name = rec_attuid_change.name, 
      t1.ROLE=rec_attuid_change.role, 
      t1.technology=rec_attuid_change.technology 
    where t1.id = rec_attuid_change.id; 
    commit; 

    update main1 t1 
    set t1.id = rec_attuid_change.id, 
      t1.name = rec_attuid_change.name, 
      t1.ROLE=rec_attuid_change.role, 
      t1.technology=rec_attuid_change.technology 
    where t1.id = rec_attuid_change.id; 
    commit; 

    update main2 t1 
    set t1.id = rec_attuid_change.id, 
      t1.name = rec_attuid_change.name, 
      t1.ROLE=rec_attuid_change.role, 
      t1.technology=rec_attuid_change.technology 
    where t1.id = rec_attuid_change.id; 
    commit; 
    end loop; 


END; 

/

回答

1

试试这个:

CREATE OR REPLACE PROCEDURE SYSTEM.process_log1 

BEGIN 

    update main t1 
    set (t1.id, t1.name, t1.ROLE, t1.technology) = 
     (select 
     sup.id as id, 
     sup.name as name, 
     sup.role as role, 
     sup.technology as technology 
     from 
     main hr, 
     secondary sup 
     where 
      sup.id=hr.id 
      and sup.Id = t1.Id); 
    commit; 

    update main1 t1 
    set (t1.id, t1.name, t1.ROLE, t1.technology) = 
     (select 
     sup.id as id, 
     sup.name as name, 
     sup.role as role, 
     sup.technology as technology 
     from 
     main hr, 
     secondary sup 
     where 
      sup.id=hr.id 
      and sup.Id = t1.Id); 
    commit; 

    update main2 t1 
    set (t1.id, t1.name, t1.ROLE, t1.technology) = 
     (select 
     sup.id as id, 
     sup.name as name, 
     sup.role as role, 
     sup.technology as technology 
     from 
     main hr, 
     secondary sup 
     where 
      sup.id=hr.id 
      and sup.Id = t1.Id); 
    commit; 


END; 

的基本思路是摆脱光标并让Oracle运行设置操作(设置为如维恩图,而不是设置为设置东西=)。它会做比RAT(一次行)快得多的速度。

我不是100%肯定的语法,但它从here,以及大约三分之一下来你有这样的页面:

SET(列名,列名,...)=(subquery4)

将按子查询4从数据库中检索到的值分配给column_name列表中的列。子查询必须返回一行,其中包含列出的所有列。子查询返回的列值按顺序分配给列表列中的列。第一个值分配给列表中的第一个列,第二个值分配给列表中的第二个列,依此类推。