It is actually less efficient to insert all rows in a single query.
First, a couple of observations:
- The amount of data to pass from client to server is the same either as one or many insert statements, where "amount of data" means the actual values you are storing.
- Hibernate supports batching of requests, so the number of round-trips between client and server can be approximately the same either as one or multiple insert statements.
Under the covers, Hibernate is using a PreparedStatement
for each query it executes on your behalf, and these are cached and reused. And MySQL caches "compile" SQL statements. Without getting mired in details, the underlying technologies are highly optimized to run a relatively small number of queries many times.
If you do the insert as a single statement, then each time that the number of values to insert is different, the new SQL has to be compiled and cached (possibly pushing another query from the cache) which adds overhead. This overhead is avoided when you just use the same SQL every time.
For many reasons, you must use bind variables in your SQL, and Hibernate will do that for you automatically. If you do some custom queries to test the all-at-once insert method, you definitely should use bind variables as well.
Another consideration is how you generate identifiers. If it is via an identity column in the database, then Hibernate needs to receive back the ID for each column, which generally is only possible when one row was created. For this reason, a sequence-based identifier generator is preferred for efficiency, with client-side caching of sequence values.
I just noticed your edit: My experience has been that Hibernate does "extra" updates when dealing with inserting parent-child data. I managed to get "pure" inserts by changing the mapping to have a "join" table (like you would see for many-to-many relationship) even though I only had a many-to-one relationship. In my case, it was significantly faster to do significantly more inserts into three tables vs. fewer inserts plus updates into two tables. If you are concerned about performance, you definitely should plan on some time to tune the Hibernate configuration.