-->
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.  [ 3 posts ] 
Author Message
 Post subject: Need Optimization Advice For One-To-One Relationship Query
PostPosted: Tue Feb 23, 2010 4:12 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
I am executing a query involving a large IN clause between two entities in a one-to-one relationship. When I execute the query in SQL the way I would do it, it runs in less than half a second. When I do it through JPA-QL with Hibernate, the length of time increases by a factor of 10. I would love some guidance as to how I can eliminate this gap.

Here is the first entity called Document:

Code:
@Entity
@Table(name = "DOC")
public class Document {
    ...


    @OneToOne(fetch = FetchType.EAGER)  //Note: Lazy is not an option for me
    @JoinColumn(name = "ARCHIVE_ID")
    public DocumentArchive getDocumentArchive() {
        return documentArchive;
    }

    public void setDocumentArchive(DocumentArchive documentArchive) {
        this.documentArchive = documentArchive;
    }

}


Here is the second entity called DocumentArchive:

Code:
@Entity
@Table(name = "DOC_ARCHIVE")
public class Archive {
    ...

    @OneToOne(mappedBy = "documentArchive")
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document= document;
    }
}


Finally, here is the JPA-QL query:

Code:
select new com.myapp.Document(doc.id, doc.subject, doc.documentArchive) from Document doc where doc.documentArchive.messageId in (:messageIds)


Note that the length of the messageIds collection could be as high as 1000.

I would love some advice on how to optimize this without resorting to a sql-result-set-mapping.

Thanks.


Top
 Profile  
 
 Post subject: Re: Need Optimization Advice For One-To-One Relationship Query
PostPosted: Wed Feb 24, 2010 8:19 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Hi,

As lazy loading is not an option for you, i dont think you can do anything else about this. Coz of the eager fetch the HQL is going to fire select for each of the Document row (in ur case 1000s).

Regards,
Litty Preeth

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Re: Need Optimization Advice For One-To-One Relationship Query
PostPosted: Wed Feb 24, 2010 8:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is probably as littypreethkr says. The query doesn't fetch the DocumentArchive objects in the main query but has to issue additional selects for each Document. You may turn on SQL logging to verify if it is so or not.

The possible solution is to try fetch join the documentArchive:

Code:
select new com.myapp.Document(doc.id, doc.subject, da) from Document doc fetch join doc.documentArchive da where da.messageId in (:messageIds)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.