mainstringargs
mainstringargs

Reputation: 14361

Hibernate Performance Tweaks

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

Upvotes: 4

Views: 1689

Answers (3)

Juggernaut
Juggernaut

Reputation: 116

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

Upvotes: 5

Brian Deterling
Brian Deterling

Reputation: 13724

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.

Upvotes: 4

duffymo
duffymo

Reputation: 308733

Using caching, cascades and lazy loading appropriately.

Tweaks? Hibernate generates SQL for you, based on the mappings you give. If you don't like the SQL, then maybe Hibernate isn't the correct tool.

The rest of performance has to do with the database design: normalization, indexes, etc.

Upvotes: 0

Related Questions