Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @DynamicUpdate(value=true) @SelectBeforeUpdate(value=true) performance

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)

updates only the modified values in the entity 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

is this 2 affirmations correct?

my main concern is.

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?

like image 804
chiperortiz Avatar asked Jan 20 '14 12:01

chiperortiz


People also ask

What is@ DynamicUpdate annotation?

Annotation Type DynamicUpdateSpecifies that SQL update statements for the annotated entity are generated dynamically, and only include columns which are actually being updated. This might result in improved performance if it is common to change only some of the attributes of the entity.

What does@ DynamicUpdate do?

Dynamic Update can help organizations and end users alike ensure that their Windows 10 devices have the latest feature update content (as part of an in-place upgrade)—and preserve precious features on demand (FODs) and language packs (LPs) that may have been previously installed.

What is @DynamicUpdate in JPA?

@DynamicUpdate is a class-level annotation that can be applied to a JPA entity. It ensures that Hibernate uses only the modified columns in the SQL statement that it generates for the update of an entity.

Does hibernate update all fields?

Hibernate always updates all database columns mapped by my entity, even the ones that I didn't change.


1 Answers

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:

  • 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.

like image 143
Chris Betti Avatar answered Sep 28 '22 23:09

Chris Betti