update 20M records in table and set col1 = col2 + col3 for each record using spring boot.
try (Connection connection = dataSource.getConnection()) {
String updateQuery = "UPDATE table SET col1 = ? WHERE id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updateQuery);
for (TableEntity entity : tableEntities) {
preparedStatement.setInt(1, calculate(offersLeadScore)); // Set new col1
preparedStatement.setObject(2, entity.getId());
preparedStatement.addBatch();
}
preparedStatement.executeBatch();`
The above approach is taking so much time to execute and update all records in table.
How to achieve faster execution using spring boot?
Tried JPA’s saveAll() and above approach. Could not optimise it.
Need some industry standard approach to update all records in one table in faster way.
2
Answers
Try this approach, it works by using a Spring Boot service called TableService to effectively update a big dataset of 20 million records inside a database table. The updateCol1ForTable function contains the main operation. This method builds a SQL update query that, using the record’s id as a reference, computes the new value for col1 as the sum of col2 and col3 for each record.
Here is the code;
To efficiently update the records in batches, call this function from your service layer or controller.
Hope it works 🙂
Why? At best you are introducing a maintenance headache. Will every developer and user remember to appropriately update
col1
whenever col2 and/or col3 is updated and every time a row is inserted. A much better solution is to perform the calculation on theselect
. It is a 1 time maintenance of the select. If for some reason you think you need to store the derived result then definecol1
as a generated. First drop the column then re-add it: (demo here)The initial run/setting will not be fast. But the trade off is you have no maintenance and the value of
col1
is automatically calculated when a row is added and whenevercol2' and/or
col3′ is updated. No additional maintenance required initially or later.