-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: reducing database roundtrips
PostPosted: Mon Aug 03, 2009 1:15 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
this may be more a JPA question than purely Hibernate related.
Say I have an @Entity A which contains a reference to another @Entity B (or worse, a List of other @Entitys).
JPA will generate first a query to retrieve A:
Code:
SELECT ID, FIELD1, B_ID FROM A WHERE ID=:ID

After which it will generate another query to retrieve the matching B:
Code:
SELECT ID, FIELD1 FROM B WHERE ID=:ID
passing in the retrieved ID.

-- SQL not identical to generated, just for documentation

Is there any way to optimise this to the SQL any human would write?:
Code:
SELECT A.ID, A.FIELD1, B.ID, B.FIELD1 FROM A A, B B WHERE B.ID = A.B_ID


Would save a lot of database roundtrips, which can be a real performance boost.


Top
 Profile  
 
 Post subject: Re: reducing database roundtrips
PostPosted: Mon Aug 10, 2009 6:44 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
There's no simple way to optimize this that won't overly complicate your mapping. You could issue HQL I guess?

Remember, this all happens inside one, single transaction, so you may have extra SQL, but not extra transactions, which is what you'd get if you just did it all by hand. It's actually fairly efficient. I think your bottlenecks will be in other locations, not here. If you believe in the 80/20 rule about performance optimization, you'll see that this isn't where to focus your attention.

-Cameron McKenze

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: reducing database roundtrips
PostPosted: Mon Aug 10, 2009 6:44 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
ooops...


Top
 Profile  
 
 Post subject: Re: reducing database roundtrips
PostPosted: Wed Aug 12, 2009 1:02 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
Thanks. I've been able to reduce things a bit by putting explicit joins on some tables into the jpql.
Won't do that for everything because of the complexity it would create of course (and because jpql doesn't support nested joins), but it reduces the overhead from big selects somewhat.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.