oracle SQL优化实战经验

1. 程序中尽量采用preparedstatement方式,减少数据库重复解析SQL的时间

2.编写SQL时要注意表联合的顺序,(oracle从右向左处理表名)建议的顺序为,将小表放在右边。(注:一般这样建议,在oracle 11g对比测试时,差异不大)

3.过滤条件中过滤记录多的条件写在最后面(注:一般这样建议,在oracle 11g对比测试时,差异不大)

4.select子句中避免使用*,最好指定字段名,对于这一点,在oracle 11g经过对比测试没有太明显的性能差异,只是一般这样建议

5.快速删除重复操作记录

  delete from emp e where rowid > ( select min(x.rowid) from emp x where x.emp_no=e.emp_no)

6.快速清空表内容

使用truncat table xxtable 的方式,而不是delete from xxtable;

7.控制commit的次数

一般建议达到一定的次数就commit一次,太频繁或者积累了太多再commit都不好;

太频繁的话,则IO高;

积累了太多再commit,则会长时间占用系统资源,如undo表空间等;

8.使用exists替代in,使用表关联方式替代exists。(注:一般这样建议,在oracle 11g对比测试时,差异不大)

 1)表关联方式

       select a.* from A a,B b where a.name=b.name

 2)exists方式

      select a.* from A a where exists (select 1 from B b where b.name=a.name)

 3)in方式

     select a.* from A a where a.name in (select name from B b)

9.查询条件如果带有is null 或者is not null,则不会使用到索引,所以写SQL时需要关注

10.如果字段上有索引,但是查询条件对字段加了函数,则不会使用到索引,对于这种情况,可以在表上对于这个字段增加函数索引。

    如 select * from A a where to_char(a.create_date,'yyyymm')='201404';  则不会使用到create_date字段上的索引;

   如果要坚持使用此SQL,可以在 to_char(a.create_date,'yyyymm')上增加索引。


参考资料:http://www.cnblogs.com/rootq/archive/2008/11/17/1334727.html



猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/24824629