간단한 자바 콘솔 응용 프로그램에서 PreparedStatement
을 사용하여 InputStream
에서 방대한 양의 데이터를로드합니다. 내가 일괄 처리를 실행하고있어최적화 방대한 양의 데이터가 포함 된 INSERT
public void readStopTimes(CSVReader reader) throws IOException, SQLException {
String insertSql = "INSERT INTO stop_times VALUES (null, ?, ?, ?, ?, ?)";
PreparedStatement statement = db.prepareStatement(insertSql);
String [] nextLine;
long i = 0;
Chronometer chronometer = new Chronometer();
while ((nextLine = reader.readNext()) != null) {
if(i++ != 0) {
statement.setString(1, nextLine[0]);
if(nextLine[1].isEmpty())
statement.setNull(2, Types.TIME);
else
statement.setTime(2, Time.valueOf(nextLine[1]));
if(nextLine[2].isEmpty())
statement.setNull(3, Types.TIME);
else
statement.setTime(3, Time.valueOf(nextLine[2]));
statement.setString(4, nextLine[3]);
statement.setInt(5, Integer.parseInt(nextLine[4]));
statement.addBatch();
}
if(i++ % 1000 == 0) {
statement.executeBatch();
}
if(chronometer.count() > 5000) {
chronometer.restart();
log.debug("Analyzed {} rows", i);
}
}
statement.executeBatch();
db.commit();
}
마다 1,000 삽입, 5 초마다 내가 로그를 인쇄하고 있습니다 :
이
는 코드입니다.로그에서 알 수 있듯이이 알고리즘은 처음에는 매우 빠르며 처음 25 초 동안 총 4 백만 개가 넘는 행을 계산 한 다음 5 초 만에 2 행만 도착하면 속도가 느려집니다 배치에 추가되었습니다.
5 백만 개가 넘는 행을 삽입해야합니다. 더 빠른 대안이 있습니까? MySQL의
innodb_flush_log_at_trx_commit = 2
또는 당신이 당신의 mysql을 만드는 것보다 충돌하지 않도록 할 수있는 경우에
답을 모르겠지만 테스트를 위해 : statement.executeBatch();'뒤에'statement = db.prepareStatement (insertSql);을 추가 할 수 있습니까? –