【发布时间】:2016-02-14 07:34:27
【问题描述】:
try (BufferedReader br = new BufferedReader(new FileReader("LoadData.txt"))){
final Connection connection = DriverManager.getConnection("jdbc:mysql://ipAddress:port/database?user=username&password=password&useServerPrepStmts=false&rewriteBatchedStatements=true");
String sCurrentLine;
final PreparedStatement ps = connection.prepareStatement("insert ignore into emails (email) value (?)");
int count = 0;
final int batchSize = 1000;
while ((sCurrentLine = br.readLine()) != null) {
ps.setString(1, sCurrentLine);
ps.addBatch();
if(++count % batchSize == 0) {
ps.executeBatch();
}
}
ps.executeBatch();
ps.close();
connection.close();
} catch (final IOException e) {
e.printStackTrace();
}
即使使用上面的代码,批量插入也非常慢,每 1000 行需要几分钟。有谁知道怎么回事?
【问题讨论】:
-
请提供
SHOW CREATE TABLE和`SHOW VARIABLES LIKE '%buffer%'。有几件事需要检查(引擎、缓存、数据类型、索引等)。
标签: java mysql sql database performance