为什么刷新对象的数量应该等于hibernate.jdbc.batch_size? [英] Why number of objects being flushed should be equal to hibernate.jdbc.batch_size?

查看:149
本文介绍了为什么刷新对象的数量应该等于hibernate.jdbc.batch_size?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如 hibernate文档在执行批量插入/更新时说,当对象的数量等于jdbc批量大小( hibernate.jdbc.batch_size )时,应刷新并清除会话。我的问题是为什么这个数字应该等于 hibernate.jdbc.batch_size 。有没有性能提示?
编辑:
例如,假设我已将 hibernate.jdbc.batch_size 设置为30在我的hibernate.cfg文件中。然后正如文档所说,当对象数量等于30时,会话应该被刷新。为什么我不应该在对象数量为20或40时进行刷新?

解决方案

JDBC批处理由以下几部分组成:


  • 将插入语句添加到批处理中,保存在内存中当您达到批处理中记录的给定数量的插入语句时(或者当没有插入语句执行时),将该批处理命令发送到数据库,并将其发送到单个网络中。
  • 为了让数据库真正执行这些插入语句,


刷新会话包括告诉Hibernate:插入保存在内存中的所有内容到数据库。因此,如果你的批处理大小设置为30,并且每冲刷5个实体,Hibernate将执行大量小批量的5个插入语句,而不是执行6次少30批次的报表。由于您已确定30是最佳批量,因此每5个实体冲洗不会使用此最佳尺寸。

如果相反,您每冲洗35个实体,那么Hibernate将执行一批30个插入,然后是一批5个,然后是一批30个,然后是一批5个,等等。再一次,你没有使用最佳的批量大小。



如果你刷新每30个实体,那么休眠将只执行最佳大小的批量,除非最后一个实体的总数不是30的倍数。


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?

解决方案

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.

这篇关于为什么刷新对象的数量应该等于hibernate.jdbc.batch_size?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆