Hibernate 性能刷新 v 提交

发布于 2024-10-30 00:07:18 字数 526 浏览 0 评论 0原文

我正在创建一个休眠组件来与大量传入数据进行交互以进行持久保存,保存(创建)和更新数据量为数百万行。

我知道刷新 v 提交的主要区别,例如刷新将“脏”数据同步到可持久的底层数据中,并且刷新允许您与底层的持久数据同步而无需实际提交,以便可以回滚事务如果需要的话。提交本质上是将所有持久数据提交到数据库。

我正在创建一个休眠组件来与大量传入数据进行交互以进行持久保存,保存(创建)和更新数据量为数百万行。

我知道刷新 v 提交的主要区别,例如刷新将“脏”数据同步到可持久的底层数据中,并且刷新允许您与底层的可持久数据同步而无需实际提交,以便可以回滚事务如果需要的话。提交本质上是将所有持久数据提交到数据库。

批量插入的合理尺寸是多少? IS 50 是合理性能的最大数量,所以类似于:

for (i < 1000000)
    if(i % 50 ) {
        session.flush()
    }

我收集 50 应该与 hibernate.jdbc.batch_size 50 中的值匹配

Im creating a hibernate component to interact with large incoming data to persist, both save(create) and update data with volumes in the million of rows.

I am aware of the main differences around flush v commit, for example flush syncing the "dirty" data into the persistable underlying data, and that flush allows you to sync with the underlying persistable data without actually committing so that the transaction can be rolled back if required. Commit essentially commits all persistable data to the database.

Im creating a hibernate component to interact with large incoming data to persist, both save(create) and update data with volumes in the million of rows.

I am aware of the main differences around flush v commit, for example flush syncing the "dirty" data into the persistable underlying data, and that flush allows you to sync with the underlying persistable data without actually committing so that the transaction can be rolled back if required. Commit essentially commits all persistable data to the database.

Whats a reasonable size to do a batch insert? IS 50 the max amount for reasonable performance so something like:

for (i < 1000000)
    if(i % 50 ) {
        session.flush()
    }

I gather 50 should match the value in the hibernate.jdbc.batch_size 50

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-11-06 00:07:18

这取决于你的数据。批量大小是休眠在其会话上保留的项目数量与往返数据库进行刷新所涉及的延迟之间的平衡。如果您的批量大小太小,您最终将需要多次往返数据库。如果您的批量大小太大,您最终将在休眠会话中保存许多对象 - 如果您的对象很胖,这可能会成为问题。

我想说 50 是一个很小的数字:1M / 50 = 20000 往返。我建议你从更大的数字开始并衡量性能。顺便说一句,这仅适用于批处理操作:hibernate.jdbc.batch_size 为 50,适用于常规应用程序事务。

PS 不要忘记在刷新后清除休眠会话,否则即使在刷新后休眠也会将持久化对象保留在内存中。

It depends on your data. The batch size is a balance between the amount of items that hibernate will keep on its session, and the latency that is involved in making roundtrips to the db for flushing. If your batch size is too small, you'll end up making many roundtrips to the db. If your batch size is too large, you will end up holding many objects in hibernate's session - this can be a problem if your objects are fat.

I would say 50 is a low number: 1M / 50 = 20000 round trips. I would say for you to start with a bigger number and measure the performance. By the way, this applies for batch operations only: hibernate.jdbc.batch_size is 50 is for regular app transactions.

PS don't forget to clear the hibernate session after flush, or else hibernate will hold the persisted objects in memory even after flush.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文