提高存储过程的性能

时间:2014-09-11 13:26:05

标签: sql stored-procedures oracle11g oracle-sqldeveloper

我是一个非数据库的人..你能否在改进下面的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 个答案:

答案 0 :(得分: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运行Set操作(如在Venn Diagrams中设置而不是Set as set something =)。它会比RAT(一次一行)更快地完成这些工作。

我对语法并不是100%肯定,但它来自here,而且大约有三分之一的页面你有这个:

SET(column_name,column_name,...)=(subquery4)

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