【问题标题】:how to make oracle for loop fast如何使oracle for循环快速
【发布时间】:2010-07-20 00:31:46
【问题描述】:

下面的查询需要 20 秒才能运行。 user_table40054 记录。 other_table14000 记录

select count(a.user_id) from user_table a, other_table b 
where a.user_id = b.user_id;

我们的限制是任何运行超过 8 秒的查询都会被终止...>_

begin
FOR i IN role_user_rec.FIRST .. role_user_rec.LAST LOOP
            SELECT COUNT (a.user_id) INTO v_into FROM user_table a
            WHERE TRIM(role_user_rec(i).user_id) = TRIM(a.user_id);
          v_count := v_count + v_into;
END LOOP;

我知道限制很糟糕,这不是effecient 做事的方式,但有没有其他方法可以让这个循环运行得更快?

【问题讨论】:

  • 您可以考虑为您的查询发布解释计划。
  • 这是如何执行的?: select count(*) from user_table a where a.user_id in (select user_id from other_table where user_id is not null)
  • role_user_rec 来自哪里?如果这是另一个查询的结果,也许可以将两者结合起来给您一个查询.... role_user_rec(i).user_id 和 user_table.user_id 是什么数据类型?

标签: oracle optimization loops


【解决方案1】:

你能绕过循环吗?我同意 Janek 的观点,如果查询本身花费的时间太长,您可能需要采用不同的方法来获取它。同意马克的观点,如果你可以在一个查询中做到这一点,那么一定要这样做。但如果你不能,请按以下方式删除循环

但是试试这样的;放弃循环:

/*
--set up for demo/test
Create Type Testusertype As Object(User_Id Number , User_Name Varchar2(500));
CREATE TYPE TESTUSERTYPETABLE IS TABLE OF TESTUSERTYPE;
*/

Declare
  Tutt Testusertypetable;
  TOTALCOUNT NUMBER ;
Begin 

    Select Testusertype(Object_Id,Object_Name)
       bulk collect into TUTT
      From User_Objects
    ;

Dbms_Output.Put_Line(Tutt.Count);

Select Count(*) Into Totalcount 
  From User_Objects Uu 
       Inner Join Table(Tutt) T
       ON T.User_Id = Uu.Object_Id;

Dbms_Output.Put_Line(Tutt.Count);
Dbms_Output.Put_Line(Totalcount);

End ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多