-->
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: query optimization - unnecessary join
PostPosted: Sat Jun 06, 2009 8:10 am 
Newbie

Joined: Sat Jun 06, 2009 7:55 am
Posts: 3
Hi,
I'm currently trying to write a query with a simple subselect on a many-to-many join table in HQL.

I have two entity classes: User and Widget.
- Widget has a many-to-one association to User called 'owner'.
- User has a many-to-many association to itself called 'friends'.

Now I want to select all Widget entities for all friends of a single user (User entity provided as parameter).

In SQL, I could do that by quering the join table directly:
Code:
SELECT id, ... FROM widgets WHERE ownerId IN (SELECT friendId FROM users_friends WHERE userId = ?)


In HQL, I managed a similar behavior using the following code:
Code:
SELECT widget
FROM User user, Widget widget
WHERE user = ?
    AND widget.owner IN elements(user.friends)


However, this causes an unnecessary join into the User table.
Resulting SQL:
Code:
        select
            widget1_.id as id0_,
            widget1_.ownerId as ownerId0_
        from
            users user0_,        << unnecessary
            widgets widget1_
        where
            user0_.id=?
            and (
                widget1_.ownerId in (
                    select
                        friends2_.friendId
                    from
                        users_friends friends2_
                    where
                       user0_.id=friends2_.userId
                )
            )
       limit ?


Is there any way to make Hibernate use the user id directly inside the subselect?

Thanks,
Marc


Top
 Profile  
 
 Post subject: Re: query optimization - unnecessary join
PostPosted: Sat Jun 06, 2009 2:35 pm 
Newbie

Joined: Wed Jun 03, 2009 10:33 am
Posts: 4
Many-to-Many relations should be divided with the introduction of an intervening table such that the many-to-many relationship is maintained via a: many-to-one <------> one-to-many relationship. Try to introduce a middle table and see if your query issues improve. HTH, David.

_________________
Yet some, not wise, go to the other side of the globe, to barbarous and unhealthy regions, and devote ten or twenty years, in that they may live,-that is, keep comfortably warm,- and die in New England at last. Henry David Thoreau - Walden - 1845


Top
 Profile  
 
 Post subject: Re: query optimization - unnecessary join
PostPosted: Mon Jun 08, 2009 11:20 am 
Newbie

Joined: Sat Jun 06, 2009 7:55 am
Posts: 3
Thanks for you reply - I will try that.


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.