在一个旧项目中,我在3列上有一个包含3个索引的表,我执行批处理查询(~= 5000更新查询),比如UPDATE mytable set mycolumn = 'blah' when myIndexedColumn = 'someId'
。
executeBatch命令,大约取1h:30,我使用oracle11g数据库,而Java6,Spring也使用。有关的表包含170万行。
Statement ps = null;
Connection connection = null;
try {
int counter = 0;
connection = myDatasource.getConnection();
connection.setAutoCommit(false);
ps = connection.createStatement();
for (String request : myListOfRequests) {
ps.addBatch(request);
}
ps.executeBatch();
connection.commit();
} catch (SQLException ex) {
LOGGER.error("My Errors : {}", ex.getMessage());
} finally {
if (ps != null) {
ps.clearBatch();
ps.close();
}
if (connection != null) connection.close();
}
我已经降低了指数,但我没有注意到有明显的差异。我不能使用现代科技。另外,删除和重建一个新表也不是一项安全的任务。你知道我怎样才能改进这个任务吗?
发布于 2020-10-25 08:41:40
解决方案,最初由jawad abbassi在问题中发布:
感谢@curiosa,我使用了preparedStatement而不是这样的语句:
PreparedStatement ps = null;
Connection connection = null;
String sql = "UPDATE MYTABLE SET COLUMN1 = ? WHERE COLUMN2 = ?";
try {
connection = myDataSource.getConnection();
connection.setAutoCommit(false);
ps = connection.prepareStatement(sql);
for (MyBean bean : myListOfBeans) {
ps.setBigDecimal(1, bean.getColumn1());
ps.setString(2, bean.getColumn2());
ps.addBatch();
}
ps.executeBatch();
} catch (SQLException ex) {
LOGGER.error("My errors : {}", ex.getMessage());
} finally {
if (ps != null) {
connection.commit();
ps.close();
connection.close();
}
}
https://stackoverflow.com/questions/64483808
复制相似问题