SQL 语句优化基础.ppt
文本预览下载声明
DECLARE n NUMBER; BEGIN n := n + 15; -- converted n := n + 15.0; -- not converted ... END; 避免隐含的数据类型转换 PL/SQL 在执行时隐含进行不同数据类型转换 Example: 当传递 PLS_INTEGER 给 NUMBER 变量时 strings dates numbers 理解 NOT NULL 约束 Oracle 系统 对 Not Null 解释 : 非空值,必需有值 NULL 在 Oracle 系统是不能解释其意义的值,比如: NULL=NULL是不成立的 Null+数字=NULL Null||字符=字符 SQL select 123+null from dual; 123+NULL ---------- SQL select abc||null from dual; AB --- abc 理解 NOT NULL 约束 Not Null 也影响到性能 : 10g/11g 多次循环 --可将循环次数加大或减少 set timing on SQL DECLARE 2 value1 NUMBER NOT NULL := 0; 3 BEGIN 4 FOR cnt IN 0 .. 1000000000 LOOP 5 value1 := 1; 6 END LOOP; 7 END; 8 / PL/SQL 过程已成功完成。 已用时间: 00: 00: 31.26 --可将循环次数加大或减少 set timing on declare value1 number; begin for cnt in 0 .. 100000000 loop value1 := 1; end loop; if value1 is not null then null; end if; end; / PL/SQL 过程已成功完成。 已用时间: 00: 00: 16.78 每次代入值前都要检测是否是非空 只需要最后对 NULL检查一次 用 PLS_INTEGER 处理整数 处理整数( integer data)使用 PLS_INTEGER : PLS_Integer比 integer 变量更高效 PLS_Integer比 INTEGER 或 NUMBER存储更省 PLS_Integer 在运行时直接采用机器的算法(machine arithmetic), 这种算法比( library arithmetic)更快 可以替代整数的NUMBER型的变量定义,而实际的处理效率是比NUMBER型更快捷 用 PLS_INTEGER 处理整数 PLS_INTEGER PL/SQL数据类型: 使用NUMBER类型的情况: SQL set serveroutput on SQL declare 2 ln_number number; 3 begin 4 dbms_output.put_line(to_char(SYSTIMESTAMP,yyyymmdd hh24:mi:ss.ff3)); 5 for n_row in 1..100000000 loop 6 ln_number := ln_number + 1; 7 end loop; 8 dbms_output.put_line(to_char(SYSTIMESTAMP,yyyymmdd hh24:mi:ss.ff3)); 9* end; SQL /14:37:57.48414:38:00.453 PL/SQL 过程已成功完成。 用 PLS_INTEGER 处理整数 PLS_INTEGER PL/SQL数据类型: 使用PLS_INTEGER类型的情况: --优化后:用PLS_INTEGER: SQL declare 2 ln_number pls_integer; 3 begin 4 dbms_output.put_line(to_char(SYSTIMESTAMP,yyyymmdd hh24:mi:ss.ff3)); 5 for n_row in 1..100000000 loop 6 ln_number := ln_number + 1; 7 end loop; 8 dbms_output.put_line(to_char(SYSTIMESTAMP,yyyymmdd hh24:mi:ss.ff3)); 9 end
显示全部