程序耗费了大量时间

时间:2013-11-28 13:00:06

标签: oracle stored-procedures plsql

我创建了这个程序。

DECLARE
    cur SYS_REFCURSOR;
    age_compare_group varchar(100);
    state_compare varchar(100);
    gender_compare varchar(100);
    income_compare_groups varchar(100);
    goal varchar(100);
    valuestring varchar(100);
BEGIN    

  OPEN cur FOR
   'select distinct a.age_compare_group, s.state_compare ,  g.gender_compare, i.income_compare_groups,goal_types.goal,goal_types.valuestring
      from age_group a ,
           state s ,
           gender g ,
           income_Group i ,
           (select ''goal'' as GOAL, valuestring
              from appldata
             where dataname like ''GOAL_CAT%''
               and datavalue in (select idcategory
                                   from goal_categories
                                  where typeuser = ''ECU''
                                and id_entity = ''B001''))goal_types';

LOOP                            

      -- process each row one at a time
      FETCH cur into age_compare_group,state_compare, gender_compare,income_compare_groups,goal,valuestring;
      insert into temp_test values ( state_compare ,age_compare_group );

 END LOOP;
    CLOSE cur;
END;

但这需要花费大量时间才能执行。

我只是想知道发生了什么,无论如何我们都可以优化它。

2 个答案:

答案 0 :(得分:2)

“一次一行”总是慢于“一次所有行”,并且不必要地使用动态SQL比使用静态SQL慢,所以为什么不尝试:

begin
      insert into temp_test values ( state_compare ,age_compare_group )
      select distinct s.state_compare, a.age_compare_group
            from age_group a ,
                 state s ,
                 gender g ,
                 income_Group i ,
                 (select 'goal' as GOAL, valuestring
                    from appldata
                   where dataname like 'GOAL_CAT%'
                     and datavalue in (select idcategory
                                         from goal_categories
                                        where typeuser = 'ECU'
                                and id_entity = 'B001'))goal_types;
end;

答案 1 :(得分:1)

您是否尝试将查询结果插入另一个表? 如果是这样,尝试按如下方式进行,

insert into temp_test select distinct s.state_compare, a.age_compare_group
  from age_group a ,
       state s ,
       gender g ,
       income_Group i ,
       (select 'goal' as GOAL, valuestring
          from appldata
         where dataname like 'GOAL_CAT%'
           and datavalue in (select idcategory
                               from goal_categories
                              where typeuser = 'ECU'
                            and id_entity = 'B001'))goal_types;