gpt4 book ai didi

Java批量插入MySQL非常慢

转载 作者:可可西里 更新时间:2023-11-01 08:54:45 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
JDBC batch insert performance

我在一个类中有以下方法:

    public void insertShingleSets(Vector<ShingleSet> shingleSets)
{
String sql = "INSERT INTO tblPostingsShingles("+
"rowId, " +
"shingle) " +
"VALUES(?,?);";

PreparedStatement statement = null;
try {
statement = conn.prepareStatement(sql);
for (int i = 0; i < shingleSets.size(); i++)
{ String id = shingleSets.get(i).getRowId();
String shingle = shingleSets.get(i).getShingle();
statement.setInt(1, Integer.parseInt(id));
statement.setString(2, shingle);
statement.addBatch();
if ((i + 1) % 1000 == 0) {
System.out.println("doing a batch " + i); //-------------
statement.executeBatch(); // Execute every 1000 items.
System.out.println("done batch " + i); //-------------
}
}
statement.executeBatch();
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}

finally {
if (statement != null)
try { statement.close();
} catch (SQLException logOrIgnore) {}
if (conn != null)
try { conn.close();
} catch (SQLException logOrIgnore) {}
}
}

执行之间的时间:System.out.println("doing a batch"+ i);并执行:System.out.println("done batch"+ i);大约 30 秒,考虑到它只是将两列插入到三列表中(另一列是自动编号主键,并且在启动/测试时表中没有行),这似乎相当长。我唯一能想到的是,在调用此方法之前,另一种方法使用检查表 tblPostingsShigles 的查询来检查是否存在某些 rowId。但是,我原以为该方法完成时会释放任何锁(它与该方法具有相同的 finally 子句)。任何建议将不胜感激。戴夫

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com