Batch Processing & Streaming
Database performance often degrades when applications issue many individual SQL statements in a loop. Each statement incurs network latency, server-side parsing, and transaction log overhead. Batch processing and streaming solve two sides of this problem: batch processing reduces the cost of writing many rows, and streaming reduces the memory cost of reading many rows.
- Batch processing groups multiple insert/update/delete operations into a single database round-trip, reducing network overhead. JDBC batching sends a prepared statement once and supplies multiple parameter sets, which the database can execute as a unit. This is significantly faster than issuing individual statements.
- Streaming processes query results row by row without loading the entire result set into memory. This is essential when result sets are too large to fit in memory, or when you want to begin processing before the query has finished returning all rows.
Batch Processing
When you pass a list of entities to Storm's insert, update, delete, or upsert methods, Storm automatically uses JDBC batch statements. The framework groups rows together and sends them to the database in a single round-trip, rather than issuing one statement per entity.
Batch Insert
- Kotlin
- Java
val users = listOf(
User(email = "alice@example.com", name = "Alice", city = city),
User(email = "bob@example.com", name = "Bob", city = city),
User(email = "charlie@example.com", name = "Charlie", city = city)
)
orm insert users
List<User> users = List.of(
new User(null, "alice@example.com", "Alice", null, city),
new User(null, "bob@example.com", "Bob", null, city),
new User(null, "charlie@example.com", "Charlie", null, city)
);
orm.entity(User.class).insert(users);
Batch Update
Pass a list of modified entities and Storm generates a batched UPDATE statement. Each entity in the list produces one row in the batch. This is especially useful when you need to apply a transformation to many rows at once.
- Kotlin
- Java
val updatedUsers = users.map { it.copy(active = true) }
orm update updatedUsers
Since Java records are immutable, you create new record instances with the modified values. Storm batches the resulting UPDATE statements.
List<User> updatedUsers = users.stream()
.map(u -> new User(u.id(), u.email(), u.name(), true, u.city()))
.toList();
orm.entity(User.class).update(updatedUsers);
Batch Delete
Batch deletes remove multiple entities in a single round-trip. Storm generates a batched DELETE using each entity's primary key.
- Kotlin
- Java
orm delete users
// Or delete all entities of a type
orm.deleteAll<User>()
orm.entity(User.class).delete(users);