JAVA 数据库编程中的性能优化

 1、 禁止自动提交:
在默认情况下,程序执行的任何sql 语句都是自动提交的
向一个表中插入2000条记录,
自动提交所用的时间  11666毫秒
禁止自动提交(显示提交) 3450毫秒

2、 批处理:
多用批处理,减少操作数据库次数。

(1)、禁止自动提交
setAutoCommit(false);
(2)、准备一个语句对象
PreparedStatement myPrepStatement = myConnection.prepareStatement(“insert into test_tab(value) values(?)”;
(3)、将语句添加进批中
addBatch();
(4)、执行这批语句
executeBatch();
(5)、提交执行的语句
myConnection.commit();


Oracle新的改进后的批处理:(JDBC2.0以上支持)
   只能对OraclePreparedStatement对象进行处理,其中的sql语句在5-30个是最优化的)
   改进的地方是,可以预设批大小,SQL 语句就会自动添加进批中。

(1)、禁止提交
(2)、设置批值
myoracleConnection.setDefaultExecuteBatch(10);
(3)、对SQL语句进行批处理
for (int count = 0;count<20;count++){
myOraclePrepStatement.setInt(1,count);
int rowsInserted = myOraclePrepStatement.executeUpdate();
}
注:还可以强制执行int rowsInserted = myOraclePrepStatement.sendBatch();


3、 行预获取
默认情况下,结果集获取的行数是10,对大多数程序都是合适的,但是,如果要获取的行非常多,那么可以增加获取尺寸以便进一步提高程序性能。
通常采用Oracle行预获取,而不用标用行预获取

标准行预获取
Statement myStatement = myConnection.CreateStatement();
myStatement.setFetchSize(20);
从数据库取2000条记录
当设为1  1642毫秒
10 161毫秒
20 91毫秒

Oracle行预获取
OracleStatement myOracleStatement = (OracleSTatement) myConntion.CreateStatement();
myOracleStatement.setRowPrefetch(20);

当设为1  1532毫秒
11 140毫秒
21 80毫秒





4、 定义结果集类型及长度
预先定义结果集列的Java类型,可以节省判断结果集类型往返。
当查询发送到数据库时,会有一次往返判断结果集应该使用的Java类型。

((OracleStatement) myStatement).defineColumnType(1,java.sql.Types.INTEGER);



5、 语句缓存
使用缓存的语句,通常可以将准备语句的时间减少一半,同时还要以避免使用结果集时创建新的游标。
两种类型: 
隐式语句缓存 
前后两次使用的语句字符串完全一样。
  显示语缓存

((OracleStatement)myPrepStatement).closeWithKey(“myCachedStatement”);


6、 数据类型定义
定义成与SQL一样的数据类型。
7、 变量名定义规则
变量大小写一至,SQL 语句字符串大小写一至。

 

>>>等值关联
select  a.id,a.title,b.columnid
from articleinfo a,articlecolumn b
where a.id=b.articlei;

>>>外关联
select  a.id,a.title,b.columnid
from articleinfo a,articlecolumn b
where a.id=b.articlei(+) and b.articleid not null;

>>>内关联
select  a.id,a.title,b.columnid
from articleinfo a,articlecolumn b
where b.articlei(+)=a.id and b.articleid not null;

>>>等值关联
select a.id,a.title 
from articleinfo a,articlecolumn b
where a.id=b.articleid;

>>>IN关联
Select a.id,a.title from articleinfo a 
Where a.id in(select articleid from articlecolumn b);

>>>等值关联 (40%)
select a.id,a.title 
from articleinfo a
where exists(select b.articleid  from articlecolumn b
where a.id=b.articleid);

>>>创建函数索引
select a.id,a.title
from articleinfo
where trunc(entertime)>=sysdate-30;

create index fun_trunc_entertime on articleinfo(trunc(entertime))

>>>显示使用某个索引
select /*+  articleinfo(fun_trunc_entertime) */ id from articleinfo
where trunc(entertime)>=sysdate-30;

>>>Where子句中的条件顺序
范围越小越靠后
select a.id,b.columnid from articleinfo a,articlecolumn b
where a.id=b.articleid(+) and b.articleid is not null and b.columnid>=353454564564576 and b.columnid<234345344565676; 
• Nested Loops (NL) Join 
• Sort-Merge Join 
• Hash Join (not available with the RBO) 
• Cluster Join 

 

posted @ 2014-12-29 10:51  R星月  阅读(261)  评论(0编辑  收藏  举报
作者:lishaoying 出处:http://www.cnblogs.com/rxingyue 说明:本文是自己学习编程的一个历程,版权归作者和博客园共有,欢迎转载,请标明原文连接,如有问题联系我 Email:983068303@qq.com,非常感谢。

作者:lishaoying
出处:http://www.cnblogs.com/rxingyue
说明:本文是自己学习编程的一个历程,版权归作者和博客园共有,欢迎转载,请标明原文连接,如有问题联系我,非常感谢。