开发者

Hibernate, DB2 - Slow running queries

I'm doing a search on one of my tables (legacy database) and recieving a horrible time here. The query is build by criteria api of hibernate, e.g.:

Criteria crit = getSessionFactory().getCurrentSession().createCriteria(P1.class);
crit.add(Restrict开发者_运维知识库ions.sqlRestriction("{alias}.compno like ?", "%" + s + "%", new StringType()));
crit.setMaxResults(25);
crit.setFirstResult(0);
crit.addOrder(Order.asc("compno"));
crit.list();

As you can see I'm already doing a paging here to improve the perfomance. This criteria needs ~6 seconds on average.

Well the native query which looks like this

select * from SCHEM.P1 where compno like '%100%' order by compno fetch first 25 rows only

takes only 10 ms which is a huge difference imo. Why does the criteria runs so slow? Need I switch back to the native sql query?

Good point on the comments:

Yes there are some relationships which I didn't had on the scope:

<set name="pI" table="P12" lazy="false">
    <key column="awcompno" update="false" />
    <one-to-many class="org.gee.hibernate.P12" not-found="ignore"/>
</set>
 <one-to-one name="info" class="org.gee.hibernate.P13" />

<set name="ma" table="P03" lazy="true" schema="SCHEMP" mutable="false" >
    <key column="macountry" property-ref="land" update="false" />
    <one-to-many class="org.gee.hibernate.P03" not-found="ignore" />
</set>


<set name="users" table="P15" lazy="true">
    <key column="apcompno" update="false" />
    <one-to-many class="org.gee.hibernate.P15" not-found="ignore"/>
</set>


My tip is:

<set name="pI" table="P12" lazy="false">
    <key column="awcompno" update="false" />
    <one-to-many class="org.gee.hibernate.P12" not-found="ignore"/>
</set>

This collection is not lazy. That may be your bottleneck.

Do you need all informations? You may read fields of your entity with hibernate, if you only want to read the IDs.


IBM pureQuery has some really nice facilities for accelerating Hibernate applications that work with DB2. The other benefit ... it makes it much easier to debug as it allows you to correlate your SQL and your Java code.

Take a look at this article http://www.ibm.com/developerworks/data/library/techarticle/dm-1008hibernateibatispurequery1/index.html


I'd say have a look at the DB logs to check what are the exact SQL instructions that get executed. Hibernate maybe loading more than just the native query you would expect, as it may load eager collections etc.

So - enable Hibernate query logging, or better yet, check the DB logs to see what gets executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜