Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Performance Tweaks

In your experience what are some good Hibernate performance tweaks? I mean this in terms of Inserts/Updates and Querying.

like image 320
mainstringargs Avatar asked Jan 23 '23 21:01

mainstringargs


2 Answers

Some Hibernate-specific performance tuning tips:

  • Avoid join duplicates caused by parallel to-many assocation fetch-joins (hence avoid duplicate object instantiations)
  • Use lazy loading with fetch="subselect" (prevents N+1 select problem)
  • On huge read-only resultsets, don't fetch into mapped objects, but into flat DTOs (with Projections and AliasToBean-ResultTransformer)
  • Apply HQL Bulk Update, Bulk Delete and Insert-By-Select
  • Use FlushMode.Never where appropriate

Taken from http://arnosoftwaredev.blogspot.com/2011/01/hibernate-performance-tips.html

like image 117
Juggernaut Avatar answered Jan 26 '23 09:01

Juggernaut


I'm not sure this is a tweak, but join fetch can be useful if you have a many-to-one that you know you're going to need. For example, if a Person can be a member of a single Department and you know you're going to need both in one particular place you can use something like from Person p left join fetch p.department and Hibernate will do a single query instead of one query for Person followed by n queries for Department.

When doing a lot of inserts/updates, call flush periodically instead of after each save or at the end - Hibernate will batch those statements and send them to the database together which will reduce network overhead.

Finally, be careful with the second level cache. If you know the majority of the objects you read by id will be in the cache, it can make things really fast, but if count on them being there but don't have it configured well, you'll end up doing a lot of single row database queries when you could have brought back a large result set with only one network/database trip.

like image 43
Brian Deterling Avatar answered Jan 26 '23 10:01

Brian Deterling