Search code examples
javaandroiddatabaseandroid-room

Room: performance of insert(Object) vs insert(List<Object>)


Does inserting object to database in the loop have lower performance than inserting list? I spent a lot of time to implement the insert(List) methods in my DAOs, but seems that I got less performance.

UPD. I implemented much more efficient packaging in lists, and insert(List)...get(List) methods shows more than 10x performance boost. I used small lists of 2-5 objects, so I could not see the performance gain


Solution

  • insert(List) will be better for performance than insert(Object). When you call insert(Object) from inside a loop, a new transaction has to be started for each insertion. In the other hand, when you use insert(List), room initiates a single transaction and insert all the rows using that transaction. Try navigating to the generated code and read the implementations.