Java 更快地执行批更新
在一个旧项目中,我有一个在3列上有3个索引的表,我正在执行批处理查询(~=5000个更新查询),比如Java 更快地执行批更新,java,oracle,spring-batch,Java,Oracle,Spring Batch,在一个旧项目中,我有一个在3列上有3个索引的表,我正在执行批处理查询(~=5000个更新查询),比如updatemytable set mycolumn='blah',myIndexedColumn='someId' executeBatch命令,大约需要1h:30,我使用Oracle11g数据库,还有Java6,SpringBatch。相关表格包含170万行 Statement ps = null; Connection connection = null; try {
updatemytable set mycolumn='blah',myIndexedColumn='someId'
executeBatch命令,大约需要1h:30,我使用Oracle11g数据库,还有Java6,SpringBatch。相关表格包含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();
}
我已经降低了指数,但我没有注意到显著的差异。我不能使用现代技术。此外,删除和重建新表不是安全任务。你知道我如何改进这项任务吗?解决方案,最初由发布在问题中的: 感谢@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();
}
}
用一批参数而不是一批语句(也)来创建一个?在db端保存对每个列的解析,并保存网络流量。确保列上有索引,并且优化器使用它们(如果您说的话),只更新1.7M行中的5k行。