JDBC大量挿入データ性能最適化
1957 ワード
, mysql5.6 batchUpdate N
:
package com.jdbc.batch;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BufferQuery {
private static Connection mysqlConn = null;
private static ResultSet rs = null;
//
private static int allCount = 10000;
//
private static int preCount = 1000;
//
private static int count = 0;
private static String insertSQL = "insert into users(firstname, lastname, age) values(?, ?, ?)";
private static PreparedStatement mysqlPs = null;
public static void main(String[] args) throws SQLException {
try {
StringBuilder sb = new StringBuilder();
sb.append("insert into users(firstname, lastname, age) values");
mysqlConn = DBUtils.getMySqlConn();
mysqlPs = mysqlConn.prepareStatement(insertSQL);
mysqlConn.setAutoCommit(false);
long start = System.currentTimeMillis();
for (int i = 1; i <= allCount; i++) {
if(i > 1) sb.append(",");
sb.append("('aa"+ i +"','bb',23)");
if(i % preCount == 0){
System.out.println(" ===>" + (++count * preCount) + " ");
}
}
mysqlPs.executeUpdate(sb.toString());
long end = System.currentTimeMillis();
System.out.println(" , : " + (end - start) + " ms");
} catch (Exception e) {
mysqlConn.rollback();
System.out.println(" , ");
throw new RuntimeException();
} finally {
mysqlConn.commit();
DBUtils.free(rs, mysqlPs, mysqlConn);
}
}
}