-->
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: Help with query optimization
PostPosted: Wed Aug 18, 2004 10:29 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
While I've got a pretty good handle on loading collections, I'm not very good at loading many-to-one relations. I was wondering what would be the best and quickest way to make business lazy on my RnsTransaction mapping. The reason I don't want to load it is because the query is carrying a lot of baggage when I really don't need it (this is true for most queries). In the below query dump, you can see that rns transaction is getting the business, it's country and it's administrator. However, my view that uses the results of this query only needs the RnsTransaction and it's histories.

HQL]
Code:
select transaction from RnsTransaction as transaction left outer join transaction.histories as history where history.date = ( select max( history.date ) from RnsTransactionHistory as history where history.rnsTransaction = transaction ) and business_id = 2 order by history.date desc


SQL Queries Being Generated:
Code:
[10:25:22,562]DEBUG SQL:237 - select rnstransac0_.id as id, rnstransac0_.business_id as business2_, rnstransac0_.cargo_control_number as cargo_co3_, rnstransac0_.transaction_number as transact4_, rnstransac0_.service_option_id as service_5_, rnstransac0_.release_office_id as release_6_, rnstransac0_.sub_location_id as sub_loca7_, rnstransac0_.container_number as containe8_ from rns_transaction rnstransac0_ left outer join rns_transaction_history histories1_ on rnstransac0_.id=histories1_.rns_transaction_id where (histories1_.date=(select max(rnstransac2_.date) from rns_transaction_history rnstransac2_ where (rnstransac2_.rns_transaction_id=rnstransac0_.id )))and(business_id=2 ) order by  histories1_.date desc limit ?
[10:25:22,625]DEBUG SQL:237 - select business0_.id as id3_, business0_.administrator_id as administ2_3_, business0_.mailbox as mailbox3_, business0_.name as name3_, business0_.address as address3_, business0_.city as city3_, business0_.province as province3_, business0_.postal_code as postal_c8_3_, business0_.country_id as country_id3_, business0_.phone_number as phone_n10_3_, business0_.fax_number as fax_number3_, business0_.account_state as account12_3_, business0_.type as type3_, administra1_.id as id0_, administra1_.name as name0_, administra1_.address as address0_, administra1_.city as city0_, administra1_.province as province0_, administra1_.postal_code as postal_c6_0_, administra1_.country_id as country_id0_, administra1_.phone_number as phone_nu8_0_, administra1_.fax_number as fax_number0_, country2_.id as id1_, country2_.name as name1_, country2_.currency_code as currency3_1_, country3_.id as id2_, country3_.name as name2_, country3_.currency_code as currency3_2_ from business business0_ left outer join administrator administra1_ on business0_.administrator_id=administra1_.id left outer join country country2_ on administra1_.country_id=country2_.id left outer join country country3_ on business0_.country_id=country3_.id where business0_.id=?
[10:25:22,687]DEBUG SQL:237 - select histories0_.id as id__, histories0_.rns_transaction_id as rns_tran2___, histories0_.id as id1_, histories0_.rns_transaction_id as rns_tran2_1_, histories0_.date as date1_, histories0_.release_code_id as release_4_1_, histories0_.edi_message as edi_mess5_1_, histories0_.delivery_instructions as delivery6_1_, releasecod1_.id as id0_, releasecod1_.name as name0_, releasecod1_.number as number0_ from rns_transaction_history histories0_ left outer join release_code releasecod1_ on histories0_.release_code_id=releasecod1_.id where histories0_.rns_transaction_id=? order by histories0_.date desc
[10:25:22,703]DEBUG SQL:237 - select histories0_.id as id__, histories0_.rns_transaction_id as rns_tran2___, histories0_.id as id1_, histories0_.rns_transaction_id as rns_tran2_1_, histories0_.date as date1_, histories0_.release_code_id as release_4_1_, histories0_.edi_message as edi_mess5_1_, histories0_.delivery_instructions as delivery6_1_, releasecod1_.id as id0_, releasecod1_.name as name0_, releasecod1_.number as number0_ from rns_transaction_history histories0_ left outer join release_code releasecod1_ on histories0_.release_code_id=releasecod1_.id where histories0_.rns_transaction_id=? order by histories0_.date desc
[10:25:22,718]DEBUG SQL:237 - select histories0_.id as id__, histories0_.rns_transaction_id as rns_tran2___, histories0_.id as id1_, histories0_.rns_transaction_id as rns_tran2_1_, histories0_.date as date1_, histories0_.release_code_id as release_4_1_, histories0_.edi_message as edi_mess5_1_, histories0_.delivery_instructions as delivery6_1_, releasecod1_.id as id0_, releasecod1_.name as name0_, releasecod1_.number as number0_ from rns_transaction_history histories0_ left outer join release_code releasecod1_ on histories0_.release_code_id=releasecod1_.id where histories0_.rns_transaction_id=? order by histories0_.date desc


Hibernate version:
2.1.6

Mapping documents: (pertinent to problem)
Code:
   <class
      name="com.upfactor.rns.domain.business.Business"
      table="business"
      >
      <id name="id" column="id" type="long" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">business_id_seq</param>
         </generator>
      </id>
      <many-to-one name="administrator" column="administrator_id" class="com.upfactor.rns.domain.business.Administrator"/>
      <property name="mailbox" column="mailbox"/>
      <property name="name" column="name"/>
      <property name="address" column="address"/>
      <property name="city" column="city"/>
      <property name="province" column="province"/>
      <property name="postalCode" column="postal_code"/>
      <many-to-one name="country" column="country_id" class="com.upfactor.rns.domain.Country"/>
      <property name="phoneNumber" column="phone_number"/>
      <property name="faxNumber" column="fax_number"/>
      <set name="persons" table="business_to_person" lazy="true">
         <key column="business_id"/>
         <many-to-many column="person_id" class="com.upfactor.rns.domain.business.Person"/>
      </set>
      <property name="accountState" column="account_state"/>
      <property name="type" column="type"/>
   </class>

   <class name="com.upfactor.rns.domain.core.RnsTransaction" table="rns_transaction">
      <id name="id" column="id" type="long" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">rns_transaction_id_seq</param>
         </generator>
      </id>
      <many-to-one name="business" column="business_id"
         class="com.upfactor.rns.domain.business.Business" />
      <property name="cargoControlNumber" column="cargo_control_number" />
      <property name="transactionNumber" column="transaction_number" />
      <many-to-one name="serviceOption" column="service_option_id" class="com.upfactor.rns.domain.core.ServiceOption"/>
      <many-to-one name="releaseOffice" column="release_office_id" class="com.upfactor.rns.domain.core.ReleaseOffice"/>
      <many-to-one name="subLocation" column="sub_location_id" class="com.upfactor.rns.domain.core.SubLocation"/>
      <property name="containerNumber" column="container_number" />
      <bag name="histories" table="rns_transaction_history" inverse="true"
         order-by="date desc" cascade="save-update">
         <key column="rns_transaction_id"/>
         <one-to-many class="com.upfactor.rns.domain.core.RnsTransactionHistory" />
      </bag>
   </class>



Name and version of the database you are using:
Postgres 7.5


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 10:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
in class com.upfactor.rns.domain.business.Business mapping file, add lazy = true and in the code a default contructor

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 12:57 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
anthony wrote:
in class com.upfactor.rns.domain.business.Business mapping file, add lazy = true and in the code a default contructor


This does seem to make business lazy on RnsTransaction, however other places where Business is used must not be lazy and thus lazy initialization errors. Here is an example from one my test cases:

Code:
      Long abcCarriersId = new Long( 2 );
      Business business = resources.getBusinessResource().find( abcCarriersId );
      business.setAccountState( AccountState.FROZEN ); // LazyInitializationException!


Is there anyway of specifiying that Business be lazy only when it is referenced by RnsTransaction objects? Or, do I have to initialize it everywhere else?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 1:05 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
In the find() method it calls load() on the session within it's dao. I just assumed since it's what I do for basic finds all the time. Sorry about that.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 1:14 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
by default: it will be lazy loaded

in the other use cases: you must query like "from Object myObj fetch join myObj.lazyLoadedManyToOneOBject

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 1:52 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
Yeah, I just figured this out myself since my other queries were working find (like findBusinessByPerson, etc.). Can I ask why Hibernate doesn't perform consistently here? Should I opt to not use load as a rule for simplicity?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 4:01 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
Anyway, this works perfect. I got my queries running at 6 milliseconds now! Better than the 30 milliseconds it was before.


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.