I'm trying to understand why saveAll has better performance than save in the Spring Data repositories. I'm using CrudRepository
which can be seen here.
To test I created and added 10k entities, which just have an id and a random string (for the benchmark I kept the string a constant), to a list. Iterating over my list and calling .save
on each element, it took 40 seconds. Calling .saveAll
on the same entire list completed in 2 seconds. Calling .saveAll
with even 30k elements took 4 seconds. I made sure to truncate my table before performing each test. Even batching the .saveAll
calls to sublists of 50 took 10 seconds with 30k.
The simple .saveAll
with the entire list seems to be the fastest.
I tried to browse the Spring Data source code but this is the only thing I found of value. Here it seems .saveAll
simply iterates over the entire Iterable
and calls .save
on each one like I was doing. So how is it that much faster? Is it doing some transactional batching internally?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…