Why number of objects being flushed should be equal to hibernate.jdbc.batch_size?

前提是你 提交于 2019-12-22 12:39:31

问题


As hibernate documentation says when doing batch inserts/updates the session should be flushed and cleared when number of objects equal to the jdbc batch size (hibernate.jdbc.batch_size). My problem is why that number should be equal to the hibernate.jdbc.batch_size. Is there a performance tip?
Edit: For an example think that I have set the hibernate.jdbc.batch_size to 30 in my hibernate.cfg file. Then As the doc says, the session should be flushed when the object count equals to 30. Why shouldn't I flush when the object count is 20 or 40?


回答1:


A JDBC batch consists of doing the following:

  • add insert statements to a batch, kept in memory
  • when you have reached a given amount of insert statements recorded in the batch (or when there are no insert statements to execute anymore), send this batch command to the database, in a single network roundtrip, in order for the database to actually execute these insert statements.

Flushing the session consists in telling Hibernate: insert everything kept in memory to the database.

So, if your batch size is set to 30, and you flush every 5 entity, Hibernate will execute lots of small batches of 5 insert statements, instead of executing 6 times less batches of 30 statements. Since you have determined that 30 was the optimal batch size, flushing every 5 entity doesn't use this optimal size.

If, on the contrary, you flush every 35 entity, then Hibernate will execute a batch of 30 inserts, then a batch of 5, then a batch of 30, then a batch of 5, etc. And once again, you're not using the optimal batch size.

If you flush every 30 entity, then hibernate will only execute batches of the optimal size, except for the last one if the total number of entities is not a multiple of 30.



来源:https://stackoverflow.com/questions/25417977/why-number-of-objects-being-flushed-should-be-equal-to-hibernate-jdbc-batch-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!