-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate incredibly slow - fix?
PostPosted: Mon Feb 09, 2004 8:05 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
I have a class A which relates to three other classes, B,C,D, using many-to-one. The relations are through property-ref.

When i call session.find("from A"), Hibernate does a select on B,C,D for _each_ instance of A.

Because of this, it takes ~20 seconds to query a few hundred objects. I coded this in JDBC and it took less than half a second, but then I was able to cache the B,C,D's manually.

The B,C,D classes are few and should be cached, but Hibernate doesn't do that. I've turned on caching, but nothing happends.

Any suggestions how to prevent the extra selects? Fetch depth doesn't help, perhaps because it's a property-ref ? ;-( Nor does Hibernate utilize the cache.


Top
 Profile  
 
 Post subject: Re: Hibernate incredibly slow - fix?
PostPosted: Mon Feb 09, 2004 8:43 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
nickvajberg wrote:
I have a class A which relates to three other classes, B,C,D, using many-to-one. The relations are through property-ref.

When i call session.find("from A"), Hibernate does a select on B,C,D for _each_ instance of A.

Because of this, it takes ~20 seconds to query a few hundred objects. I coded this in JDBC and it took less than half a second, but then I was able to cache the B,C,D's manually.

The B,C,D classes are few and should be cached, but Hibernate doesn't do that. I've turned on caching, but nothing happends.

Any suggestions how to prevent the extra selects? Fetch depth doesn't help, perhaps because it's a property-ref ? ;-( Nor does Hibernate utilize the cache.


Use lazy="true" for your set's in the mapping file. This way when you load A you won't load B, C and D.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 8:50 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Can I set lazy on many-to-one relations? I thought this was for collections only.

I _have_ tried lazy proxies on all classes, but it doesn't help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 11:49 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
You're correct you can't put lazy on a many-to-one but you CAN put lazy on the b,c and d classes.

eg.

Code:
<class name="B" table="b" lazy="true" >


Don't worry about specifying proxies it will automatically create one for you when you add this flag. Now when you load/get/find one or more a objects b,c and d will not be loaded.

If you access them later ( a.b ) then the database will be queried then for that single b object.

Be careful, accessing proxied classes outside the session the parent object was loaded in will cause exceptions.

Cheers.

Myk.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 1:28 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Oh... as I said:

"I _have_ tried lazy proxies on all classes, but it doesn't help."

SO?


Top
 Profile  
 
 Post subject: mess with the outer-join settings
PostPosted: Mon Feb 09, 2004 3:07 pm 
Newbie

Joined: Wed Jan 21, 2004 5:51 pm
Posts: 14
Location: San Francisco, CA
Hibernate won't fetch related objects unless you instruct it to do so. Configure the fetch depth in your hibernate.properties file:
Code:
hibernate.max_fetch_depth 2

... the fetch depth controls how deeply hibernate recurses into your object heirarchy to eagerly fetch related objects. The mapping file has an attribute, outer-join="[true/false]", on relationship elements to determine whether or not outer joins are allowed. Read the documentation for information about the performance tradeoffs of outer joins. A rule of thumb I learned from another OR tool states that your fetch depth should be at most 3, and you should never enable outer-joining for two or more one-to-many relationships on a single object.

I believe some databases will not suport outer joins (MySQL, maybe?) I know that MS SQL Server and Oracle both support outer joins.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 6:14 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
I have used fetch size before, but in 2.1.2 there appears to be a bug. If my fetch size > 0, I get:

"SybSQLException: Select expression results in more than one column having same name. Column name 'name17_' is specified more than once"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.