-->
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.  [ 3 posts ] 
Author Message
 Post subject: [SOLVED] A rather simple query which executes slow...
PostPosted: Tue Jun 16, 2009 5:06 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
I've got a rather simple HQL query. This query retrieves about 4000 entities from a database. The query takes
2656 ms. to execute with Hibernate, but only 110 ms. when i execute the translated SQL query through a MySQL
database tool.

1. I've tried to remove everything that has to do with subclassing in the mapping file. No performance improvement.
2. I've tried to change the ID from java.util.UUID to a regular String. No performance improvement.
3. I've tried to remove the many-to-one reference to society. No performance improvement.
4. I've tried to remove the order by clause in the HQL statement. No performance improvement.
5. I've disabled anything that has to do with cache, to avoid any overhead. No performance improvement.

I'd be very happy to receive any tips on how i might improve this query! There must be some way to get this query
to perform better than almost 3 seconds?

HQL query:
Code:
from Stakeholder s left join fetch s.society order by s.name


SQL query:
Code:
select stakeholde0_.id as id9_0_, society1_.id as id13_1_, stakeholde0_.updated as updated9_0_, stakeholde0_.name as name9_0_, stakeholde0_.cae as cae9_0_, stakeholde0_.status as status9_0_, stakeholde0_.flag as flag9_0_, stakeholde0_.society_id as society8_9_0_, stakeholde0_.type as type9_0_, society1_.updated as updated13_1_, society1_.name as name13_1_, society1_.status as status13_1_, society1_.flag as flag13_1_ from stakeholder stakeholde0_ left outer join society society1_ on stakeholde0_.society_id=society1_.id order by stakeholde0_.name


Mapping File
Code:
<class name="com.tracker.db.entities.Stakeholder" table="stakeholder">
     <id column="id" length="36" name="id" type="com.tracker.db.usertypes.CustomUUID"/>
     <discriminator column="type" type="java.lang.String"/>
     <version column="updated" name="updated" type="java.util.Calendar" unsaved-value="null"/>
     <property column="name" length="150" name="name" type="java.lang.String"/>
     <property column="cae" length="15" name="cae" type="java.lang.Integer"/>
     <property column="status" length="1" name="status" not-null="true" type="java.lang.Integer"/>
     <property column="flag" length="1" name="flag" not-null="true" type="java.lang.Integer"/>
     <many-to-one class="com.tracker.db.entities.Society" column="society_id" name="society"/>

     <subclass discriminator-value="PA" name="com.tracker.db.entities.Patronym" />
     <subclass discriminator-value="PS" name="com.tracker.db.entities.Pseudonym" />
     <subclass discriminator-value="GR" name="com.tracker.db.entities.Group" />
</class>


Statistics:
Code:
** Query Statistics **
Qyery Execution Max Time: 2656
Qyery Execution Count   : 1
Entity Load Count       : 3866
Collection Fetch Count  : 0

** Cache Statistics **
Qyery Cache Hit Count   : 0
Qyery Cache Put Count   : 0
Qyery Cache Miss Count  : 0


Last edited by sbrattla on Tue Jun 16, 2009 3:57 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: A rather simply query which executes horribly slow...
PostPosted: Tue Jun 16, 2009 7:14 am 
Newbie

Joined: Fri May 16, 2008 3:40 pm
Posts: 13
sbrattla wrote:
I've got a rather simple HQL query. This query retrieves about 4000 entities from a database. The query takes
2656 ms. to execute with Hibernate, but only 110 ms. when i execute the translated SQL query through a MySQL
database tool.
Does the 110ms include fetching all 4000 rows from the db? When you time the HQL execution, do the 2.6s include creating 8000 Java objects and setting the attributes of those objects from the database rows? It looks to me, like you are comparing apples to oranges.


Top
 Profile  
 
 Post subject: Re: A rather simply query which executes horribly slow...
PostPosted: Tue Jun 16, 2009 10:51 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Indeed, it does look like I have been comparing apples to oranges. I did not take into account all the work Hibernate does in the background,
and for a large dataset of about 4000 entities - that seems to be quite some work.

I solved this by only retrieving a list of scalar values (id, name) from the database. The data model does not load the element from the
database before a view actually requests it. In other words, a custom kind of "lazy loading". This might not be a 100% approved object oriented approach,
but then again i'm now able to retrieve all 4000 elements from the database in about 700 ms, as opposed to nearly 3000 ms. before. I believe the new
approach also scales much better.


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