Oracle 这个过程耗费了很多时间

Oracle 这个过程耗费了很多时间,oracle,stored-procedures,plsql,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 OPE

我创建了这个过程

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;
但执行起来需要很多时间

我只想知道发生了什么,我们可以通过任何方式对其进行优化。

“一次一行”总是比“一次所有行”慢,并且不必要地使用动态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;
“一次一行”总是比“一次所有行”慢,而且不必要地使用动态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;
“一次一行”总是比“一次所有行”慢,而且不必要地使用动态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;
“一次一行”总是比“一次所有行”慢,而且不必要地使用动态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;

是否尝试将查询结果插入到另一个表中? 如果是这样的话,试着按如下方式做:

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;

是否尝试将查询结果插入到另一个表中? 如果是这样的话,试着按如下方式做:

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;

是否尝试将查询结果插入到另一个表中? 如果是这样的话,试着按如下方式做:

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;

是否尝试将查询结果插入到另一个表中? 如果是这样的话,试着按如下方式做:

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;


您的查询不进行任何联接。你正在创建一个州、性别、收入和嵌套选择之间的笛卡尔乘积——难怪它运行缓慢。另外:你根本不需要光标。但是首先要让你的
select
也正确:开始使用显式
JOIN
语法,而不是容易出错的隐式连接-如果你从一开始就这样做了,你就不会创建笛卡尔积。是的,因为这是我想要的。你想要所有这些表之间的笛卡尔积吗?你明白那是什么吗?我非常怀疑你是否真的想要那样。如果您这样做了,那么为什么要使用
distinct
子句消除由于笛卡尔积而创建的所有重复项?这基本上不是一个无限循环,因为您没有退出条件,例如当cur%NOTFOUND您的查询不进行任何连接时退出。你正在创建一个州、性别、收入和嵌套选择之间的笛卡尔乘积——难怪它运行缓慢。另外:你根本不需要光标。但是首先要让你的
select
也正确:开始使用显式
JOIN
语法,而不是容易出错的隐式连接-如果你从一开始就这样做了,你就不会创建笛卡尔积。是的,因为这是我想要的。你想要所有这些表之间的笛卡尔积吗?你明白那是什么吗?我非常怀疑你是否真的想要那样。如果您这样做了,那么为什么要使用
distinct
子句消除由于笛卡尔积而创建的所有重复项?这基本上不是一个无限循环,因为您没有退出条件,例如当cur%NOTFOUND您的查询不进行任何连接时退出。你正在创建一个州、性别、收入和嵌套选择之间的笛卡尔乘积——难怪它运行缓慢。另外:你根本不需要光标。但是首先要让你的
select
也正确:开始使用显式
JOIN
语法,而不是容易出错的隐式连接-如果你从一开始就这样做了,你就不会创建笛卡尔积。是的,因为这是我想要的。你想要所有这些表之间的笛卡尔积吗?你明白那是什么吗?我非常怀疑你是否真的想要那样。如果您这样做了,那么为什么要使用
distinct
子句消除由于笛卡尔积而创建的所有重复项?这基本上不是一个无限循环,因为您没有退出条件,例如当cur%NOTFOUND您的查询不进行任何连接时退出。你正在创建一个州、性别、收入和嵌套选择之间的笛卡尔乘积——难怪它运行缓慢。另外:你根本不需要光标。但是首先要让你的
select
也正确:开始使用显式
JOIN
语法,而不是容易出错的隐式连接-如果你从一开始就这样做了,你就不会创建笛卡尔积。是的,因为这是我想要的。你想要所有这些表之间的笛卡尔积吗?你明白那是什么吗?我非常怀疑你是否真的想要那样。如果你这样做了,那么为什么你要使用
distinct
子句消除笛卡尔积产生的所有重复项?这基本上不是一个无限循环,因为你没有退出条件,比如当cur%NOTFOUND时退出。正如我所同意的,我怀疑这会提高性能。问题是select语句似乎完全错误。我还假设查询是错误的,这个简单的
选择不同的s.state\u compare,a.age\u compare\u group from age\u group a,state s
应该返回与您相同的输出,正如我所同意的,我怀疑这会提高性能。问题是select语句似乎完全错误。我还假设查询是错误的,这个简单的
选择不同的s.state\u compare,a.age\u compare\u group from age\u group a,state s
应该返回与您相同的输出,正如我所同意的,我怀疑这会提高性能。问题是select语句似乎完全错误。我还假设查询是错误的,这个简单的
选择不同的s.state\u compare,a.age\u compare\u group from age\u group a,state s
应该返回与您相同的输出,正如我所同意的,我怀疑这会提高性能。问题是select语句似乎完全错误。我还假设查询是错误的,这个简单的语句
select distinct s.state\u compare,