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: new to hibernate+performance
PostPosted: Fri Apr 07, 2006 3:11 am 
Newbie

Joined: Fri Apr 07, 2006 2:33 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="entity.Address" table="address">


<id name="id" column="id" type="integer" access="field">
<generator class="increment"/>
</id>

<property
name="street"
column="street"
type="string"
access="field"/>
<property
name="apt"
column="apt"
type="string"
access="field"/>
<property
name="date"
column="date"
type="java.util.Date"
access="field"/>
<many-to-one
name="user"
column="user_id"
class="entity.User"
access="field"
lazy="false"
/>
</class>

</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="entity.User" table="user" >

<id name="id" column="id" type="integer" access="field">
<generator class="increment"/>
</id>

<property
name="name"
column="name"
type="string"
access="field"/>
<property
name="surname"
column="surname"
type="string"
access="field"/>
<property
name="pass"
column="pass"
type="string"
access="field"/>
<property
name="type"
column="type"
type="string"
access="field"/>

<set name="addresses" cascade="all" lazy="false" fetch="join" access="field">
<key column="user_id"/>
<one-to-many class="entity.Address"/>
</set>
</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySql 4.0.15

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

First of all i must say that i am new to hibernate.
I make two classes name User and Address and make biderectional relation between them.
in the database there are 100 users and for each of these users there are 1000 addresses (test code).

when i get the users (address also since non-lazy) like below

Session session = null;
Transaction tx = null;
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery( "from User" );
List list=null;
try{
list = query.list();
session.close();
}
catch(Exception ex){
ex.printStackTrace();
}

it takes too many times (nearly 10 seconds). what is wrong here.
Before sending this message i make a search on the forum but cannot solve the problem.


Last edited by shire on Mon Apr 10, 2006 2:16 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 7:49 am 
Newbie

Joined: Fri Apr 07, 2006 2:33 am
Posts: 4
i couldnot solve this problem.
actually i dont know whether it is a problem or not.
i know there are some solutions like second-level cache.
but in my application, at some point i need to read these much of record (they arenot in the second-level cache) and it is to slow (5-6 time slower than jdbc)
is there any way to make these query faster?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 9:00 am 
Newbie

Joined: Fri Apr 07, 2006 2:33 am
Posts: 4
still couldnot solve the problem
please help.


Top
 Profile  
 
 Post subject: Don't use fetchmode=join on addresses
PostPosted: Thu Apr 13, 2006 9:53 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
What's happening here is when you define fetchmode=join on the addresses, you end up joining both you user table and address table and the relationship is no longer lazy. If each user hase 10 addresses, you're getting back 10,000 User instances + addresses from your query. By removing the fetch mode, you'll see a dramatic speed improvement. Try turning on SQL logging so you can see what is happening:

http://www.hibernate.org/hib_docs/v3/re ... on-logging


The join fetchmode is most useful in a single-ended association, or if you use it to eagerly fetch a one-to-many association. Hope this helps.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 10:16 am 
Newbie

Joined: Fri Apr 07, 2006 2:33 am
Posts: 4
thnx damnhandy.
before sending the first message i have also tried the no-fetch version you mentioned.
there is performance increase when i make it without fetch but it is still slow.


Top
 Profile  
 
 Post subject: try setting a batch size
PostPosted: Thu Apr 13, 2006 10:34 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Try also setting a batch size on the user class, like batch-size="100". Do this in the class mapping. Also turn on SQL logging too, this will show what Hibernate is creating for a query and how it is executing the query. It will give you some insight as to why it is so slow.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 1:00 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
One other thing to consider: if your running this query inside of a JUnit, make sure you account for Hibernate to build its configuration. This can take a fair amount of time depending on the number of mappings you have.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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.