今天在做一個(gè)將excel數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的程序時(shí),由于數(shù)據(jù)量大,準(zhǔn)備采用jdbc的批量插入。于是用了preparedStatement.addBatch();當(dāng)加入1w條數(shù)據(jù)時(shí),再執(zhí)行插入操作,preparedStatement.executeBatch()。我原以為這樣會(huì)很快,結(jié)果插入65536條數(shù)據(jù)一共花30多分鐘,完全出乎我的意料。于是問了一下同事,他們在處理這種大批量數(shù)據(jù)導(dǎo)入的時(shí)候是如何處理的,發(fā)現(xiàn)他們也是用的jdbc批量插入處理,但與我不同是:他們使用了con.setAutoCommit(false);然后再preparedStatement.executeBatch()之后,再執(zhí)行con.commit();于是再試,什么叫奇跡?就是剛剛導(dǎo)入這些數(shù)據(jù)花了半小時(shí),而加了這兩句話之后,現(xiàn)在只用了15秒鐘就完成了。于是去查查了原因,在網(wǎng)上發(fā)現(xiàn)了如下一段說明:
* When importing data into InnoDB, make sure that MySQL does not have autocommit mode enabled because that
requires a log flush to disk for every insert. To disable autocommit during your import operation, surround it with
SET autocommit and COMMIT statements:
SET autocommit=0;
... SQL import statements ...
COMMIT;
第一次,正是因?yàn)闆]有setAutoCommit(false);那么對于每一條insert語句,都會(huì)產(chǎn)生一條log寫入磁盤,所以雖然設(shè)置了批量插入,但其效果就像單條插入一樣,導(dǎo)致插入速度十分緩慢。
部分代碼如下:
String sql = "insert into table *****";con.setAutoCommit(false);ps = con.prepareStatement(sql);for(int i=1; i<65536; i++){ ps.addBatch(); // 1w條記錄插入一次 if (i % 10000 == 0){ ps.executeBatch(); con.commit(); }}// 最后插入不足1w條的數(shù)據(jù)ps.executeBatch();con.commit();