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 informationTables 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:
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:
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.