Java 休眠 @DynamicUpdate(value=true) @SelectBeforeUpdate(value=true) 性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21233853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate @DynamicUpdate(value=true) @SelectBeforeUpdate(value=true) performance
提问by chiperortiz
i am start using this 2 hibernate annotations in my APP.
我开始在我的 APP 中使用这 2 个休眠注释。
@DynamicUpdate(value=true)
@SelectBeforeUpdate(value=true)
first i will try to explain what i understand about it to know if i am right about it.
首先,我将尝试解释我对它的理解,以了解我是否正确。
@DynamicUpdate(value=true)
updates only the modified values
in the entity Hibernate needs to track those changes
仅更新modified values
实体中的Hibernate needs to track those changes
@SelectBeforeUpdate(value=true)
creates a select
before update
to know which properties has been changed this is useful when the entity has been loaded and updated on different sessions Hibernate is out of tracking entity changes
创建select
beforeupdate
以了解哪些属性已更改,这在实体已在不同会话上加载和更新时很有用Hibernate is out of tracking entity changes
is this 2 affirmations correct?
这2个肯定正确吗?
my main concern is.
我主要关心的是。
in DB performance
which is better or faster updates all the fields in the entity at onceor generate a select to know which columns update and update only the modified columns?
在DB performance
哪一个更好或更快的一次更新的实体的所有字段或generate a select to know which columns update and update only the modified columns?
采纳答案by Chris Betti
The situation depends on your circumstance. If your table is very simple (has no foreign key constraints, only few columns, few indexes), then updating the full record is going to be faster.
情况取决于您的情况。如果您的表非常简单(没有外键约束,只有很少的列,很少的索引),那么更新完整记录会更快。
If, however, your table has many foreign key constraints and indexes, it will be faster to first select and then update the differences. This is because PostgreSQL has to do the following work for each column in the update:
但是,如果您的表有许多外键约束和索引,则先选择然后更新差异会更快。这是因为 PostgreSQL 必须为更新中的每一列做如下工作:
- Check foreign key constraint
- Update related indexes
- 检查外键约束
- 更新相关索引
Furthermore, the changes add bloat to the tables which must be cleaned up by vacuum.
此外,这些更改增加了必须通过真空清理的表的膨胀。
Keep in mind that if you use dynamicUpdate on a database with many tables, and your updates look very different, you'll start evicting cached query plans. Those plans cost resources to compute fresh. Though, cached plans might only be useful to subsequent queries in the same session anyhow.
请记住,如果您在具有许多表的数据库上使用 dynamicUpdate,并且您的更新看起来非常不同,您将开始驱逐缓存的查询计划。这些计划消耗资源来计算新鲜。尽管如此,缓存计划可能只对同一会话中的后续查询有用。