jdbc插入大量数据时优化处理-最近遇到的解决我一大难题
来源:互联网 发布:如何更新mac os系统 编辑:程序博客网 时间:2024/05/20 02:54
转载地址:http://blog.sina.com.cn/s/blog_894ed5190100z7wr.html
这个对我来说确实收获不小,一次性插入上万条数据,确实好使。
使用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();
}
}
wordend 相关阅读:
性能测试(并发负载压力)测试分析
软件性能测试的重要性及策略
软件性能测试入门
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%100==0){//适应于插入的数据结果为百整数(10000 or 100000 etc)?if(i%100 == 0 || i == count(上传文件的总数量)-1){ //解决余数小于100 的问题。
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向数据库插入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();
}
}
wordend 相关阅读:
性能测试(并发负载压力)测试分析
软件性能测试的重要性及策略
软件性能测试入门
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%100==0){//适应于插入的数据结果为百整数(10000 or 100000 etc)?if(i%100 == 0 || i == count(上传文件的总数量)-1){ //解决余数小于100 的问题。
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插入大量数据时优化处理-最近遇到的解决我一大难题
- 【Java/JDBC.ORM】 jdbc插入大量数据时优化处理
- 【Java/JDBC.ORM】 jdbc插入大量数据时优化处理
- jdbc插入大量数据时优化处理(转)
- JDBC中使用批处理处理大量的插入数据操作
- 用JDBC向数据库中插入大量数据时的优化
- Mysql大量插入数据时SQL语句的优化
- Mysql大量插入数据时SQL语句的优化
- JDBC大数据插入性能优化对比
- sqlite大量数据插入的优化
- JDBC+批处理 插入大量数据
- jdbc大量插入数据(prepareStatement)
- redis 大量数据的插入处理
- JDBC解决插入数据库时遇到的引号问题
- 使用JDBC插入大量数据的性能测试
- 使用JDBC插入大量数据的性能测试
- 使用JDBC插入大量数据的性能测试
- 使用JDBC插入大量数据的性能测试
- java工具
- 2015年双11手机淘宝前端技术之H5性能最佳实践
- block与协议-代理
- UVA 10401 Injured Queen Problem
- 百度地图API学习笔记(二)
- jdbc插入大量数据时优化处理-最近遇到的解决我一大难题
- 多些时间能少写些代码
- MySQL与Oracle 差异比较之函数
- Android中的Application类
- KVC与KVO
- Pymongo: TypeError: if no direction is specified, key_or_list must be an instance of list
- 高通平台手机开发之Bring-up
- 多线程编程
- Kafka+Storm+HDFS整合实践