浅谈Oracle性能优化可能出现的问题

运维 数据库运维 Oracle
本文是作者在进行Oracle优化时碰到的一些问题,并由此进行了一些改进的尝试。并得出结论,Oracle中的exit跳出了外层的循环。

笔者在看Oracle性能优化一书时,讲到用exists替代in,用表链接替代exists,关于前者,一般效果比较明显,exists效率明显比in高,但是如果要想表连接的效率比exists高,必须在from子句中,将记录多的表放在前面,记录少的表放后面。

关于select... bulk collect into ... limit ...或fetch... bulk collect into ... limit ...句型:

在使用如上句型时,通常我们都会用for或forall循环进行insert/update/delete操作。

for/forall循环方法有好几种,如  

第1种:

for tp in tmp.FIRST.. tmp.LAST loop   
....  
end loop;

第2种:

for tp in 1 .. tmp.COUNT loop   
....  
end loop; 

第3种:

for tp in indecs of tmp loop   
....  
end loop;

上面的第1种方法有一个致使的弱点,就是在select... bulk collect into ... limit ...或fetch... bulk collect into ... limit ...没有取到数据时,如果没有exit,则第一种方法会报错:ORA-06502: PL/SQL: numeric or value error。因为tmp.FIRST访问不存在,为空值。必须对错误进行错误处理。而在嵌套的循环中,内层的循环是不能用exit的,所有必然遇到这种错误。

第2种方法不会出现这种问题,第3种方法没有试过。

借鉴网上的做法,给出一种使用绑定变量的批量删除数据的存储过程:

    PROCEDURE RemoveBat2DjaRecords(参数)    
    AS  
        type RowIdArray is table of rowid index by binary_integer;         
        rowIds RowIdArray;  
    BEGIN  
        loop  
            select rowid BULK COLLECT into rowIds from 表名  
            where 查询条件 and rownum <= 1000;     
            exit when SQL%NOTFOUND;  
        forall k in 1 .. rowIds.COUNT  
        delete from 表名 where rowid = rowIds(k);   
            commit;  
        end loop;   
    EXCEPTION  
        when OTHERS then                  
            rollback;  
    END RemoveBat2DjaRecords; 

上面的1000条是一个可以设定的数,根据你的服务器性能可以扩大或缩小。

用exit跳出循环,通常情况下,exit只跳出当前层的循环,与其它程序设计语言的break语句类似。在嵌套的循环中,如果要直接从内层循环跳出外面多层的循环,可使用'EXIT 标签 When'形式的语句,举例如下:

SQL>   BEGIN   
  2          <>  
  3          FOR v_outerloopcounter IN 1..2 LOOP  
  4               <>  
  5               FOR v_innerloopcounter IN 1..4 LOOP  
  6                    DBMS_OUTPUT.PUT_LINE('Outer Loop counter is ' 
  7                         || v_outerloopcounter ||  
  8                         ' Inner Loop counter is ' || v_innerloopcounter);  
  9                         EXIT WHEN v_innerloopcounter = 3;  
 10              END LOOP innerloop;  
 11         END LOOP outerloop;  
 12    END;  
 13    /  
Outer Loop counter is 1 Inner Loop counter is 1  
Outer Loop counter is 1 Inner Loop counter is 2  
Outer Loop counter is 1 Inner Loop counter is 3  
Outer Loop counter is 2 Inner Loop counter is 1  
Outer Loop counter is 2 Inner Loop counter is 2  
Outer Loop counter is 2 Inner Loop counter is 3  
 
PL/SQL procedure successfully completed.  

从上面可以看出,普通情况下,exit只跳出当前层的循环。

SQL>   BEGIN   
  2          <>  
  3          FOR v_outerloopcounter IN 1..2 LOOP  
  4               <>  
  5               FOR v_innerloopcounter IN 1..4 LOOP  
  6                    DBMS_OUTPUT.PUT_LINE('Outer Loop counter is ' 
  7                         || v_outerloopcounter ||  
  8                         ' Inner Loop counter is ' || v_innerloopcounter);  
  9                         EXIT outerloop WHEN v_innerloopcounter = 3;  
 10              END LOOP innerloop;  
 11         END LOOP outerloop;  
 12    END;  
 13    /  
Outer Loop counter is 1 Inner Loop counter is 1  
Outer Loop counter is 1 Inner Loop counter is 2  
Outer Loop counter is 1 Inner Loop counter is 3  
 
PL/SQL procedure successfully completed. 

从上面可以看出,exit跳出了外层的循环。 

【编辑推荐】

  1. Oracle数据库开发经验浅谈
  2. 详解Oracle几种性能调优方法
  3. Oracle DBA职责及日常工作分析
责任编辑:彭凡 来源: CSDN博客
相关推荐

2011-05-20 10:05:13

Oracle性能优化

2009-05-04 09:52:49

Oracle优化排序

2010-04-14 12:51:10

Oracle性能

2011-07-18 18:01:34

buffer cach

2009-06-29 10:19:42

.NET Micro性能优化

2009-06-30 11:23:02

性能优化

2018-01-09 16:56:32

数据库OracleSQL优化

2011-08-02 21:16:56

查询SQL性能优化

2021-03-21 23:43:22

线程编程安全

2010-04-13 16:08:24

Oracle数据插入

2010-11-15 16:20:33

Oracle系统优化

2017-08-25 15:28:20

Oracle性能优化虚拟索引

2010-04-07 09:21:03

Oracle RAC

2010-05-05 11:48:27

Oracle设计开发阶

2010-04-21 17:09:28

Oracle启动模式

2010-04-23 14:48:26

Oracle性能优化

2010-04-21 12:49:57

Oracle性能

2015-05-22 10:17:47

tableView性能优化

2023-12-29 12:12:04

广告性能优化

2009-06-30 16:08:19

性能问题代码写法
点赞
收藏

51CTO技术栈公众号