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.  [ 6 posts ] 
Author Message
 Post subject: Performance Problem - Hibernate generating too many queries
PostPosted: Fri Sep 12, 2008 5:47 pm 
Newbie

Joined: Fri Sep 12, 2008 5:39 pm
Posts: 5
Can anyone help me figure out why Hibernate is generating thousands of queries when I expect to to only generate a handful of queries?

We are building a web application using JSF, Hibernate, Spring and several other frameworks. We are using the Java Annotations for Hibernate and although we are getting the correct results the performance of our system is dismal. Hibernate issues thousands of queries for what something that I could do with one short SQL query.

Can you give me some suggestions of things to look for?

Anyone have any insight of what may be happening?

Is there some kind of a profiling or debugging mode that may help us figure out what is going on?


Thanks,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 6:07 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
In order to evaluate how many queries are generated, you don't need any special profiler tool . Just log (using log4j) any output at DEBUG level coming from the package org.hibernate.SQL. That will allow you to log all SQL statements to a different file, or to the console if you want.

You can reduce the amount of generated SQL by changing your fetching strategy. Your abundance of SQL queries is most likely caused by child collections performing one query per collection element.
Try adding the property fetch="join" to the mapping files of your child collections.

If you are using Hibernate queries or HQL, try adding the "fetch" keyword to your queries. This approach is preferable to modifying the mapping file directly, because it is something you can do (or not) on a case-by-case basis.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 7:02 pm 
Newbie

Joined: Fri Sep 12, 2008 5:39 pm
Posts: 5
Thanks for the quick response! I noticed the problem looking at the server logs from MySQL and I think that your correct about it performing one query per collection element.

I'm going to try adding the fetch="join" attribute and see if that fixes the problem.

Thanks,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 2:11 pm 
Newbie

Joined: Fri Sep 12, 2008 5:39 pm
Posts: 5
I found a resource on the web that describes hibernate's loading strategies:

http://www.hibernate.org/hib_docs/annot ... ntity.html
http://www.hibernate.org/315.html

The first document provides an annotation:
@[One|Many]ToOne](fetch=FetchType.EAGER) @LazyToOne(FALSE) @Fetch(JOIN)

i.e. For the join cols you can add an annotation like:
@ManyToOne(fetch=FetchType.EAGER)



Many of the join cols in my application already have this annotation. Is there something else that I should be adding?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 6:54 pm 
Newbie

Joined: Fri Sep 12, 2008 5:39 pm
Posts: 5
I also noticed that our our application performed a number of HQL queries and I added the fetch keyword to the query but didn't notice any improvements. There are still hundreds or thousands of queried being issued.

FYI. Here's a doc that discusses using fetch in HQL queries.
http://www.hibernate.org/hib_docs/refer ... ryhql.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 4:21 pm 
Newbie

Joined: Fri Sep 12, 2008 5:39 pm
Posts: 5
I'm still having problems.

I changed to use fetch=join and it only had a minimal impact on the number of queries issued.

Does anyone have any other suggestions?


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