Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate performance

 
Robert Siqueira
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys !

What is the best approach about hibernate fetch strategy, use LAZY or EAGER ? Is it better get all data in only once SQL (EAGER) or some SQL queries when you need the data (LAZY) ??

Regards,

Robert
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on the data and how you use it, there is no "one size fits all" approach to this.
 
Robert Siqueira
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ddo you have some source where I can study about this kind of performance hints ??

tks...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Hibernate docs are a good place to start.
 
Antonio Gomes Rodrigues
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:The Hibernate docs are a good place to start.


Or if you understand French

http://arodrigues.developpez.com/tutoriels/java/performance/hibernate-performance-part1-strategies-chargement/
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The worst performance comes when you have LAZY on data that you will be using. So let's say your database has a USERS table and a related ADDRESS table. You retrieve a list of users, and you want to print out their addresses. Hibernate will do one query for the list of users and then one more query for the addresses for each of the users. That's a total of N+1 queries, where N is the number of users. Also know as the N+1 problem, this represents an epic failure in your mappings.

On the other hand, you could put EAGER on the addresses, but what if you rarely actually care about that data. The Hibernate query for users will include a join with the address table, which of course will be slower than a simple query of the users table. If you don't access the addresses, then you've wasted the effort to retrieve them.

Now, it is possible to write Hibernate (and JPA) queries to explicitly join in the addresses where you want them. That will override the LAZY load in the mappings. However, if you forget to do it, then you get the N+1 problem again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic