默认情况下禁用 hibernate 批处理 / hibernate.order_updates / hibernate.order_inserts 是否有任何原因?当您启用 50 的批量大小时有什么缺点吗? order_updates / order_inserts 参数相同。是否存在不应启用这些功能的用例?使用这些功能时会对性能产生影响吗?
我只能看到,当我需要减少查询计数时,这些设置很有帮助,尤其是在应用程序和数据库服务器之间延迟较高的云环境中。
一般情况下,将
batch size
设置为合理大小,并将order_insert
、order_updates
设置为true
可以显着提高性能。
在我的所有项目中,我都使用此配置作为基础:
hibernate.jdbc.batch_size = 100
hibernate.order_inserts = true
hibernate.order_updates = true
hibernate.jdbc.fetch_size = 400
但是,是的 - 使用批处理时可能会产生“内存影响”。但这取决于jdbc驱动程序。 例如,Oracle JDBC 驱动程序为每个
PreparedStatement
创建内部缓冲区并重用这些缓冲区。如果您调用简单的更新语句,您可以使用
ps.setInt(1, ...)
、ps.setString(2, ...)
等设置一些参数,Oracle 会将这些值转换为某种字节表示形式,并存储在与此 PreparedStatement
和连接关联的缓冲区中。但是,当您的 PreparedStatement
使用大小为 100 的批次时,此缓冲区将增大 100 倍。如果你有一些连接池,例如有 50 个连接,那么可能有 50 个这样大的缓冲区。如果您有 100 个不同的语句使用批处理,那么所有此类缓冲区都会产生显着的内存影响。当您启用批量大小时,它会成为全局设置 - Hibernate 将使用它进行所有插入/更新。
但是,我发现在我的所有项目中,性能增益比内存影响更重要,这就是为什么我使用batchsize=100
作为默认值。
对于order_inserts
、
order_updates
,我认为默认情况下这些设置是禁用的,因为这些设置仅在批处理打开时才有意义。启用批处理后,这些排序只是开销。您可以在Oracle的白皮书中找到更多信息:
http://www.oracle.com/technetwork/topics/memory.pdf。====编辑2016年5月31日====
关于order_inserts
order_udpates
属性的一句话。 假设我们有实体
A
、
B
并以这种方式持久化 6 个对象:
session.save(A1); // added to action queue
session.save(B1); // added to action queue
session.save(A2); // ...
session.save(B2); // ...
session.save(A3); // ...
session.save(B3); // ...
执行上述操作后:
这 6 个对象已生成标识符
案例1:
order_inserts = false
在刷新阶段,hibernate 执行6 insert 语句:
ActionQueue = [A1, B1, A2, B2, A3, B3]
insert into A - (A1)
insert into B - (B1)
insert into A - (A2)
insert into B - (B2)
insert into A - (A3)
insert into B - (B3)
情况2:
order_inserts = true
,允许批量
现在,在刷新阶段,hibernate 执行2 批插入语句:
ActionQueue = [A1, A2, A3, B1, B2, B3]
insert into A - (A1, A2, A3)
insert into B - (B1, B2, B3)
此处的文档: