休眠 @DynamicUpdate(value=true) @SelectBeforeUpdate(value=true) 性能

2022-01-17 00:00:00 sql-update select java Hibernate

我开始在我的 APP 中使用这 2 个休眠注释.

i am start using this 2 hibernate annotations in my APP.

@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)

仅更新实体中的修改值 Hibernate 需要跟踪这些更改

@SelectBeforeUpdate(value=true)

update 之前创建一个 select 以了解哪些属性已更改,这在实体已在不同会话上加载和更新时很有用 Hibernate 已退出跟踪实体变化

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

这两个肯定正确吗?

我主要关心的是.

DB 性能 中哪个更好或更快 一次更新实体中的所有字段 或 生成一个选择以了解哪些列更新并仅更新修改列?

in DB performance which is better or faster updates all the fields in the entity at once or generate a select to know which columns update and update only the modified columns?

推荐答案

具体情况视你的情况而定.如果您的表非常简单(没有外键约束,只有很少的列,很少的索引),那么更新完整记录会更快.

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.

但是,如果您的表有许多外键约束和索引,则首先选择然后更新差异会更快.这是因为 PostgreSQL 必须为更新中的每一列做以下工作:

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:

  • 检查外键约束
  • 更新相关索引

此外,这些更改增加了必须通过真空清理的表的膨胀.

Furthermore, the changes add bloat to the tables which must be cleaned up by vacuum.

请记住,如果您在具有许多表的数据库上使用 dynamicUpdate,并且您的更新看起来非常不同,您将开始逐出缓存的查询计划.这些计划花费资源来计算新的.不过,缓存的计划可能只对同一会话中的后续查询有用.

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.

相关文章