使用JDBC插入大量数据时,约20w条insert语句,最开始的代码:
Connection conn = null;
Statement st = null;
try{
conn = dba.getConnection();
st = conn.createStatement();
long start = System.currentTimeMillis();
if (batchSql.endsWith("/r/n"))
{
batchSql.subSequence(0, batchSql.length() - 2);
}
String[] datas = batchSql.split(m_strLines);
for(int i=0;i<datas.length;i++){
st.addBatch(datas[i]);
if(i%4000 == 0){
st.executeBatch();
}
}
st.executeBatch();
logger.info("-----------------------批量执行sql所需时间为:--"+(System.currentTimeMillis()-start)+" 毫秒");
}catch(Exception e){
logger.info("------------------------>批量执行sql方法出错了"+e);
}finally{
dba.close(st);
dba.close(conn);
}
执行的时间超乎想象,在网上查找优化方法,
加上conn.setAutoCommit(false);,在每次批量执行完提交一次,20万的数据仅仅用了37s