jdbc 批量增加方式 性能测试
来源:互联网 发布:ui美工培训 编辑:程序博客网 时间:2024/06/05 02:57
2009-05-21
使用JDBC插入大量数据的性能测试
关键字: 性能测试
使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:
//1.使用statement插入100000条记录
public void exec(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//设置手动提交
Statement st = conn.createStatement();
for(int i=0;i<100000;i++){
String sql="insert into t1(id) values ("+i+")";
st.executeUpdate(sql);
}
Long endTime = System.currentTimeMillis();
System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//2.使用PreparedStatement对象
public void exec2(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//手动提交
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=0;i<100000;i++){
pst.setInt(1, i);
pst.execute();
}
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//3.使用PreparedStatement + 批处理
public void exec3(Connection conn){
try {
conn.setAutoCommit(false);
Long beginTime = System.currentTimeMillis();
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=1;i<=100000;i++){
pst.setInt(1, i);
pst.addBatch();
if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等
pst.executeBatch();
conn.commit();
pst.clearBatch();
}
}
Long endTime = System.currentTimeMillis();
System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在Oracle 10g中测试,结果:
1.使用statement耗时142秒;
2.使用PreparedStatement耗时56秒;
3.使用PreparedStatement + 批处理耗时:
a.50条插入一次,耗时5秒;
b.100条插入一次,耗时2秒;
c.1000条以上插入一次,耗时1秒;
通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。
使用JDBC插入大量数据的性能测试
关键字: 性能测试
使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:
//1.使用statement插入100000条记录
public void exec(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//设置手动提交
Statement st = conn.createStatement();
for(int i=0;i<100000;i++){
String sql="insert into t1(id) values ("+i+")";
st.executeUpdate(sql);
}
Long endTime = System.currentTimeMillis();
System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//2.使用PreparedStatement对象
public void exec2(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//手动提交
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=0;i<100000;i++){
pst.setInt(1, i);
pst.execute();
}
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//3.使用PreparedStatement + 批处理
public void exec3(Connection conn){
try {
conn.setAutoCommit(false);
Long beginTime = System.currentTimeMillis();
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=1;i<=100000;i++){
pst.setInt(1, i);
pst.addBatch();
if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等
pst.executeBatch();
conn.commit();
pst.clearBatch();
}
}
Long endTime = System.currentTimeMillis();
System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在Oracle 10g中测试,结果:
1.使用statement耗时142秒;
2.使用PreparedStatement耗时56秒;
3.使用PreparedStatement + 批处理耗时:
a.50条插入一次,耗时5秒;
b.100条插入一次,耗时2秒;
c.1000条以上插入一次,耗时1秒;
通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。
0 0
- jdbc 批量增加方式 性能测试
- JDBC批量插入数据的性能测试
- spring 批量增加实现方式
- JDBC批量操作性能提升
- jdbc数据库性能测试
- jdbc 加载性能测试
- Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案
- Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案
- Mysql 批量insert性能测试
- Mycat批量插入性能测试
- 性能测试学习方式
- 银行批量代收付性能测试总结
- SQL批量数据导入,性能测试
- elasticsearch 批量入库bulk性能测试
- mysql三种批量增加的性能分析
- Jmeter性能测试,MySQL JDBC request
- Jmeter jdbc和接口性能测试
- 服务器性能测试方式、方法
- php扩展接收参数问题
- Oracle List Partition Table
- 每日一道算法题:a~z包括大小写与0~9组成的N个数,用最快的方式把其中重复的元素挑出来。
- UDP接收多路视频数据, 接收缓存不足了
- UVA - 1602(Polyomino的存储和判重)
- jdbc 批量增加方式 性能测试
- MYSQL外键(Foreign Key)的使用
- do{...}while(0)的意义和用法
- 招聘网站转型在即 面视网领跑行业创新
- vim哲学
- flex 浏览器不读取swf缓存文件
- iOS工程如何支持64-bit
- **(leetcode) Binary Tree Zigzag Level Order Traversal (tree)
- 大话设计模式之建造者模式