Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Batch Insert. Would it ever use one insert instead of multiple inserts?

I've been looking around trying to determine some Hibernate behavior that I'm unsure about. In a scenario where Hibernate batching is properly set up, will it only ever use multiple insert statements when a batch is sent? Is it not possible to use a DB independent multi-insert statement?

I guess I'm trying to determine if I actually have the batching set up correctly. I see the multiple insert statements but then I also see the line "Executing batch size: 25."

There's a lot of code I could post but I'm trying to keep this general. So, my questions are:

1) What can you read in the logs to be certain that batching is being used?

2) Is it possible to make Hibernate use a multi-row insert versus multiple insert statements?

like image 595
AHungerArtist Avatar asked May 29 '11 07:05

AHungerArtist


1 Answers

Hibernate uses multiple insert statements (one per entity to insert), but sends them to the database in batch mode (using Statement.addBatch() and Statement.executeBatch()). This is the reason you're seeing multiple insert statements in the log, but also "Executing batch size: 25".

The use of batched statements greatly reduces the number of roundtrips to the database, and I would be surprised if it were less efficient than executing a single statement with multiple inserts. Moreover, it also allows mixing updates and inserts, for example, in a single database call.

I'm pretty sure it's not possible to make Hibernate use multi-row inserts, but I'm also pretty sure it would be useless.

like image 126
JB Nizet Avatar answered Oct 26 '22 11:10

JB Nizet