---
title: "JAVA 数据库编程中的性能优化"
author: "fanjava"
date: "2005-03-16T20:46:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2005-03-16T20:46:00Z"
id: 601770
url: "https://fanjava.iteye.com/blog/601770"
---

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

2、 批处理：  
多用批处理，减少操作数据库次数。  
（1）、禁止自动提交  
setAutoCommit(false);  
（2）、准备一个语句对象  
PreparedStatementmyPrepStatement=myConnection.prepareStatement(“insertintotest\_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(intcount=0;count&lt;20;count++){  
myOraclePrepStatement.setInt(1,count);  
introwsInserted=myOraclePrepStatement.executeUpdate();  
}  
注：还可以强制执行introwsInserted=myOraclePrepStatement.sendBatch();

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

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

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

当设为11532毫秒  
11 140毫秒  
21 80毫秒

4、 定义结果集类型及长度  
预先定义结果集列的Java类型，可以节省判断结果集类型往返。  
当查询发送到数据库时，会有一次往返判断结果集应该使用的Java类型。  
((OracleStatement)myStatement).defineColumnType(1,java.sql.Types.INTEGER);

5、 语句缓存  
使用缓存的语句，通常可以将准备语句的时间减少一半，同时还要以避免使用结果集时创建新的游标。  
两种类型：  
隐式语句缓存  
前后两次使用的语句字符串完全一样。  
显示语缓存  
（（OracleStatement）myPrepStatement）.closeWithKey(“myCachedStatement”);  
6、 数据类型定义  
定义成与SQL一样的数据类型。  
7、 变量名定义规则  
变量大小写一至，SQL语句字符串大小写一至。

&gt;&gt;&gt;等值关联  
selecta.id,a.title,b.columnid  
fromarticleinfoa,articlecolumnb  
wherea.id=b.articlei;

&gt;&gt;&gt;外关联  
selecta.id,a.title,b.columnid  
fromarticleinfoa,articlecolumnb  
wherea.id=b.articlei(+)andb.articleidnotnull;

&gt;&gt;&gt;内关联  
selecta.id,a.title,b.columnid  
fromarticleinfoa,articlecolumnb  
whereb.articlei(+)=a.idandb.articleidnotnull;

&gt;&gt;&gt;等值关联  
selecta.id,a.title  
fromarticleinfoa,articlecolumnb  
wherea.id=b.articleid;

&gt;&gt;&gt;IN关联  
Selecta.id,a.titlefromarticleinfoa  
Wherea.idin(selectarticleidfromarticlecolumnb);

&gt;&gt;&gt;等值关联（40％）  
selecta.id,a.title  
fromarticleinfoa  
whereexists(selectb.articleidfromarticlecolumnb  
wherea.id=b.articleid);

&gt;&gt;&gt;创建函数索引  
selecta.id,a.title  
fromarticleinfo  
wheretrunc(entertime)&gt;=sysdate-30;

createindexfun\_trunc\_entertimeonarticleinfo(trunc(entertime))

&gt;&gt;&gt;显示使用某个索引  
select/\*+articleinfo(fun\_trunc\_entertime)\*/idfromarticleinfo  
wheretrunc(entertime)&gt;=sysdate-30;

&gt;&gt;&gt;Where子句中的条件顺序  
范围越小越靠后  
selecta.id,b.columnidfromarticleinfoa,articlecolumnb  
wherea.id=b.articleid(+)andb.articleidisnotnullandb.columnid&gt;=353454564564576andb.columnid&lt;234345344565676;  
• NestedLoops(NL)Join  
• Sort-MergeJoin  
• HashJoin(notavailablewiththeRBO)  
• ClusterJoin