-->
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.  [ 1 post ] 
Author Message
 Post subject: Join query too slow when using table persubclass inheritance
PostPosted: Mon Oct 09, 2006 5:45 pm 
Newbie

Joined: Sat Aug 19, 2006 7:15 am
Posts: 2
Hibernate version:
3.2rc5

Mapping documents:
Code:
<?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 package="net.myorg.persistence.pojo">
   <class name="Document" table="Document" mutable="false" lazy="true">
      <cache usage="read-only" />
      <id name="unid" type="string" length="32" column="dcm_unid">
         <generator class="assigned" />
      </id>
      <property name="identifier" column="dcm_identifier"/>
      <!-- other attributes -->       

   
      <joined-subclass name="News" table="News" lazy="true">
         <key column="dcm_unid" />
         <property name="fio" column="nws_fio"/>
         <!-- other attributes -->
      </joined-subclass>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
   Criteria query = session.createCriteria(News.class).setMaxResults(2);
   List news = query.list();


Name and version of the database you are using:
IBM DB2 8.2 (Service pack 13)

The generated SQL (show_sql=true):
Code:
select * from (
   select rownumber() over() as rownumber_, // and other attributes
      from LIGAOL.News_big this_ inner join LIGAOL.Document_big this_1_ on this_.dcm_unid=this_1_.dcm_unid
   ) as temp_
   where rownumber_ <= 2


Other information
Tables Document and News has about 150 000 rows both.
Generated SQL is working about 8 seconds


My comments:
With this generated SQL there is a very bad plan at DB2 server:
Image
So, as you can see, DB2 uses MSJOIN (Merge Join) instead of NLJOIN (Nested Loops Join). So, it sorts two tables without indexes and joins all 150 000 rows. And only after this it filters first 2 rows.

If i would run query:
Code:
select rn, Document.*, News.*from (
   select * from
      (select dcm_unid , rownumber() over(order by d.dcm_viewFrom desc) as rn               
         from Document d) as t1       
      where rn <= 2) as t2     
   join News on News.dcm_unid = t2.dcm_unid join Document on Document.dcm_unid = t2.dcm_unid;


it works very quickly, and its plan is:
Image

So, how can I say hibernate to use my own query for inheritance join? Can I ask hibernate generate two queries for join (like fetch="select")?
Is there a way to speed up table per subclass inheritance join?

Help me please!

Thanks!

P.S.: Sorry for my bad English.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.