micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
469 stars 198 forks source link

Make `saveAll` use batch inserts #3193

Open ritratt opened 1 month ago

ritratt commented 1 month ago

Feature description

Using Micronaut-Data 4.6.x. I'm trying to persist multiple rows to my table.

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface MyRepository extends PageableRepository<Foo, Long> {
}

// And then I call the `saveAll` to save multiple rows from somewhere else
public void saveData(List<Foo> lostOfData) {
  myRepository.saveAll(lotsOfData);
}

I expected Micronaut to do a batch insert using a single connection+transaction. However, it turns out it not only does one insert per element of Foo but it also uses an individual DB connection. This results in performance issues as well as a starved connection pool.

I checked the docs and couldn't find a way to do this without writing my own queries, but then that defeats the purpose of using out of the box features that Micronaut Data provides.

graemerocher commented 1 month ago

this should be the case, batch inserts are supported https://github.com/micronaut-projects/micronaut-data/blob/4.10.x/data-jdbc/src/main/java/io/micronaut/data/jdbc/operations/DefaultJdbcRepositoryOperations.java#L1296

Note that some databases don't support returning the ID for a batch insert.

Can you provide an example that reproduces the issue?