I came across a scenario where I have to insert 20 rows into a database (SQLite). The approach I followed was to create an update
method which would take a String
and a float
and set them in a PreparedStatement
. I used to call this update method 20 times to insert all the rows. This is my update method:
void update(String variableName, int value) throws SQLException {
String query = "INSERT OR REPLACE INTO Table1 (STRINGNAME, VALUE) VALUES(?,?)";
try (Connection connection = getDataSource().getConnection();
PreparedStatement pst = connection.prepareStatement(query);) {
pst.setString(1, variableName);
pst.setInt(2, value);
pst.executeUpdate();
}
}
I got to know that there is a provision to add multiple queries in a batch and execute the whole batch using addBatch
and executeBatch
methods provided by PreparedStatement
.
As per my understanding, addBatch
will add an SQL query to the current list of commands for the Statement
object and executeBatch
will execute all the queries in the list. Isn't this equivalent to calling the update
method multiple times (Other than maybe the initialisation of PreparedStatement
)? Is there any performance advantage in using addbatch
and executeBatch
over executing the update
method multiple times?
Update
I tested both the scenarios: one with batch update and one with a modified version of the update
method for 20000 queries.
updateCommon
, modified update
method with common PreparedStatment
:
void updateCommon(String variableName, int value, PreparedStatement pst) throws SQLException {
pst.setString(1, variableName);
pst.setInt(2, value);
pst.executeUpdate();
}
updateBatch
method, where data is added to batch:
void updateBatch(String variableName, float value, PreparedStatement pst) throws SQLException {
pst.setString(1, variableName);
pst.setInt(2, value);
pst.addBatch();
}
Testing
updateCommon
test was done as follows:
try {
Connection conn = getDataSource().getConnection();
PreparedStatement pst = conn.prepareStatement(query);
for (int i = 0; i < 1000; i++) {
updateCommon("String1" + i, 1, pst);
/*
* till 20
*/
updateCommon("String20" + i, 20, pst);
}
}
updateBatch
test was done as follows:
try (Connection conn = getDataSource().getConnection(); PreparedStatement pst = conn.prepareStatement(query); ) {
for (int i = 0; i < 1000; i++) {
updateBatch("String1" + i, 1, pst);
/*
* till 20
*/
updateBatch("String20" + i, 20, pst);
}
pst.executeBatch();
}
Result:
Time taken by updateCommon
method for completion of test: 117.2 seconds
Time taken by updateBatch
method for completion of test: 267.6 seconds
This further makes the question more interesting as it seems like, direct update with a common PreparedStatement
is almost twice faster in this specific use case.
Therefore, Is there any advantage in using addBatch() and executeBatch() over executing multiple queries sequentially?