[英]Fastest 'update' on jdbc with PreparedStatement and executeBatch
I have a java program that in some circumstances must update a large amount of records in a database (eg 100,000). 我有一个Java程序,在某些情况下,该程序必须更新数据库中的大量记录(例如100,000个)。
The way it does it is by creating a PreparedStatement
and by using the addBatch
technique. 它的创建方式是通过创建
PreparedStatement
并使用addBatch
技术。 Here is the snippet: 这是代码段:
connection.setAutoCommit(false);
PreparedStatement ps = connection.prepareStatement(
"UPDATE myTable SET colName=? where id=?");
for (...) { // this loop can be 100000 long
colValue = ...
id = ...
ps.setString(1,colValue);
ps.setString(2,id);
ps.addBatch();
}
ps.executeBatch();
connection.commit();
is this the best (fastest) way to update 100000 of records in JDBC ? 这是在JDBC中更新100000条记录的最佳(最快)方法吗?
Could anybody suggest a better way ? 有人可以提出更好的方法吗?
Try this as a benchmark: 尝试以此为基准:
Use the built-in SQL tools to do a bulk extract of the entire table. 使用内置的SQL工具对整个表进行批量提取。 All rows.
所有行。 All columns.
所有列。
Drop (or rename) the table. 删除(或重命名)表。
Use a simple flat-file read/write to create a new file with the updates applied. 使用简单的平面文件读/写来创建应用了更新的新文件。
Use the bulk-load utility that comes with your database to rebuild the entire table from the extracted file. 使用数据库附带的bulk-load实用程序从提取的文件重建整个表。
Add indexes after the reload. 重新加载后添加索引。
You may find that this is faster than any SQL solution. 您可能会发现这比任何SQL解决方案都快。 We stopped using UPDATES for a data warehouse because extract -> flat file process -> load was much faster than SQL.
我们停止对数据仓库使用UPDATES,因为提取->平面文件处理->加载比SQL快得多。
Since batching uses buffering on client side and then sends everything as a single request, it might be wise to execute batches with 5000 rows. 由于批处理在客户端使用缓冲,然后将所有内容作为单个请求发送,因此明智的做法是执行具有5000行的批处理。 You should watch you memory consumption when adding 100.000 rows.
添加100.000行时,您应该注意内存消耗。
Sometime it works faster to push data in several loads instead of 1 single load(using JDBC, at least based on my previous experience). 有时,以几次加载而不是一次加载加载数据的速度更快(至少使用JDBC,至少基于我以前的经验)。
dont use for loop use spring jdbc templete 不要用于循环使用Spring JDBC Templete
public void Update(final List<Aclass> aclasss) {
String sql = "UPDATE myTable SET colName=? where id=?";
GenericDAO.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Aclass aclass= aclasss.get(i);
ps.setString(1,colValue);
ps.setString(2,id);
}
@Override
public int getBatchSize() {
return aclasss.size();
}
});
}
您应该将Spring Batch操作与JdbcTemplate一起使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.