避免在oracle中挂起会话

时间:2019-10-04 07:24:54

标签: oracle plsql cursor

我在pl / sql中有一个程序,可以处理少量数据。问题是,当有大量数据时,cursor6有时会挂起会话,而其他用户无法访问table5。 table5有一个主键列“ ocf”,它是用户ID,因此每个用户只能访问对应于其用户ID的行。即使在这种情况下,cursor6也会锁定table5并创建一个死会话,我不知道如何,但是它不允许其他用户访问它 即使他们使用table5中的其他行。是否有解决方案来重新创建该游标以进行更新或使用可能做同样事情的方式,从而避免挂起会话?

cursor cursor6(pf varchar2,PO number, psu varchar2) is select * from table5 where ocf=PO and .. for update;
row1 cursor6%rowtype;
BEGIN
   delete table5 where ocf=PO;
    commit;
    open cursor1;
    fetch cursor1 into wv,wd;
    if cursor1%found then
    open cursor2;
      fetch cursor2 into wf;
      if cursor2%found then
      while cursor2%found loop
          open cursor3;
          fetch cursor3 into wco;
          if cursor3%found then
            while cursor3%found loop
                open cursor5;
                  fetch cursor5 into ws;
                  while cursor5%found loop
                       open cursor4;
                       fetch cursor4 into ..;
                       if cursor4%found then
                            open cursor6(..);
                            fetch cursor6 into row1;
                            if cursor6%notfound then insert into table5 values (..);
                            else update table5 set ... where current of cursor6;    
                            end if;
                            close cursor6;
                       end if;
                       close cursor4;
                   end loop;
                close cursor5;
            end loop;
          end if;
          close cursor3;
        end loop;
       end if;
     close cursor2;
    end if;
    close cursor1;
    commit;
    delete table5 where ocf=PO;
    commit;

1 个答案:

答案 0 :(得分:3)

天哪,祝你好运,有6个嵌套循环。


  

cursor6锁定table5并创建一个死会话,我不知道如何,但是它不允许其他用户访问

这种情况发生在未索引外键列的情况下。我建议您检查是否是这种情况,如果是,请创建索引。


这是一个查询(作者是 Tom Kyte )(抱歉,我没有原始文档的链接;如果需要,可以使用Google的链接),该查询显示未编制索引的外键约束。我通过添加什么进行显示对其进行了一些修改:

  • PAR_WHAT
    • 0-仅显示丢失的内容
    • 1-显示有效的
    • NULL-全部显示

我建议您首先使用PAR_WHAT = 0运行它。

WITH forkey
     AS (SELECT DECODE (b.table_name, NULL, '****', 'ok') Status,
                a.table_name,
                a.columns column_1,
                b.columns column_2
           FROM (  SELECT SUBSTR (a.table_name, 1, 30) table_name,
                          SUBSTR (a.constraint_name, 1, 30) constraint_name,
                             MAX (
                                DECODE (position,
                                        1, SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        2, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        3, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        4, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        5, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        6, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        7, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        8, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        9, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   10, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   11, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   12, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   13, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   14, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   15, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   16, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                             columns
                     FROM user_cons_columns a, user_constraints b
                    WHERE     a.constraint_name = b.constraint_name
                          AND b.constraint_type = 'R'
                 GROUP BY SUBSTR (a.table_name, 1, 30),
                          SUBSTR (a.constraint_name, 1, 30)) a,
                (  SELECT SUBSTR (table_name, 1, 30) table_name,
                          SUBSTR (index_name, 1, 30) index_name,
                             MAX (
                                DECODE (column_position,
                                        1, SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        2, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        3, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        4, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        5, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        6, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        7, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        8, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        9, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   10, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   11, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   12, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   13, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   14, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   15, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   16, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                             columns
                     FROM user_ind_columns
                 GROUP BY SUBSTR (table_name, 1, 30),
                          SUBSTR (index_name, 1, 30)) b
          WHERE     a.table_name = b.table_name(+)
                AND b.columns(+) LIKE a.columns || '%'
                AND a.table_name NOT LIKE 'HEP_DP%')
  SELECT f.status,
         f.table_name,
         f.column_1,
         f.column_2
    FROM forkey f
   WHERE f.status =
            CASE
               WHEN :par_what = 1 THEN 'ok'
               WHEN :par_what = 0 THEN '****'
               ELSE f.status
            END
ORDER BY f.table_name, f.column_1, f.column_2;