2.数据访问优化方法——时间(效率)换空间
2.1:索引优化
http://tech.sina.com.cn/s/2009-11-26/00481148854.shtml
2.2:预编译——减少编译次数
2.3:手动提交——减少读写磁盘次数
2.4:批处理——减少传递次数
2.5:查询——动态绑定与静态绑定
2.5.1:效率——静态>动态变量绑定SQL>动态字符串拼接SQL
3.性能测试代码。
3.1package com.lovo.batch;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class TestBatch4 {
public static void main(String[] args)throws Exception {
//首先要注册驱动程序(理解:读取jar包,即安装驱动)
Class.forName("oracle.jdbc.driver.OracleDriver");
//指定连接数据库时所需要的信息
String url="jdbc:oracle:thin:@192.168.1.20:1521:orcl";
String user="scott";
String pwd="tiger";
Connection conn = DriverManager.getConnection(url, user, pwd);
conn.setAutoCommit(false);
String sql="truncate table emp";
Statement st = conn.createStatement();
st.executeUpdate(sql);
sql="INSERT INTO emp(empno,ename) VALUES (?, ?)";
PreparedStatement pst = conn.prepareStatement(sql);
long time1 = System.currentTimeMillis();
//传递(网络)n/10000次,编译(CPU)1次,提交(写硬盘)n/10000次
for (int i = 1;i<10000;i++){
pst.setInt(1, i+10000);
pst.setString(2, "a"+i);
pst.addBatch();
//不同的服务器要调整每次传递数据量
if(i%10000==0){
pst.executeBatch();
conn.commit();
}
}
pst.close();
conn.close();
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
}