10. 优化GROUP BY:
提高GROUP BY 语句的效率, 可以通过将不需要的记录在GROUP BY 之前过滤掉.下面两个查询返回相同结果,但第二个明显就快了许多.
低效: SELECT JOB , AVG(SAL) FROM EMP
GROUP BY JOB
HAVING JOB = ‘PRESIDENT’ OR JOB = ‘MANAGER‘;
高效:SELECT JOB , AVG(SAL) FROM EMP
WHERE JOB=‘PRESIDENT'OR JOB = ‘MANAGER'
GROUP BY JOB;
11. 判断是否为“空”只能用is null或is not null,严禁使用比较运算符进行判断
:不应编写成
if ( item1 = null ) then
…
elsif ( item2 <> '' ) then
…
else
…
end if;
应编写如下:
if ( item1 is null ) then
…
elsif ( item2 is not null ) then
…
else
…
end if;
12. 用number定义数字型字段
不要使用integer定义数字型字段,integer默认是38位,尽量使用number(n,m)定义数字型字段
13. 尽量不要使用动态SQL
只有在字段名,表名,数据库名之类作为变量时,才必须用动态SQL ,其它情况应尽量少使用动态SQL,动态SQL是执行的时候才进行编译的,这样会影响执行速度
动态语句写法:
str2:='select(:x,:y,:z) into aa,bb,cc from :h';
execute immediate str2 using a,b,c,table_name;