-
Notifications
You must be signed in to change notification settings - Fork 231
Running queries in a batch
Lars Aaberg edited this page Aug 19, 2017
·
1 revision
If you need to run an UPDATE, INSERT or DELETE query multiple times with different parameters, you will get a huge performance gain by running them in a batch. This is by some databases also known as bulk updates or bulk inserts. Sql2o supports only homogeneous batches, that is batches with only one query, but with multiple sets of parameters. This is due to limitations in jdbc.
In the example below, 100 rows are inserted into a table in the database in one batch.
public void insertABunchOfRows(){
final String sql = "INSERT INTO SomeTable(id, value) VALUES (:id, :value)";
try (Connection con = sql2o.beginTransaction()) {
Query query = con.createQuery(sql);
for (int i = 0; i < 100; i++){
query.addParameter("id", i).addParameter("value", "foo" + i)
.addToBatch();
}
query.executeBatch(); // executes entire batch
con.commit(); // remember to call commit(), else sql2o will automatically rollback.
}
}
It is almost always a good idea to run batch updates in a transaction, as batch updates are not atomic and therefor might leave your database in an inconsistent state.
Contributing
Documentation
- Configuration
- Fetching data from database
- Fetching data lazily
- Column mappings
- Updates and inserts
- Transactions
- Running queries in a batch
- Integration with Spring Framework
- Register custom converters
- Sql2o on Android
Other resources