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: Optimizing too many joins
PostPosted: Fri Jul 30, 2010 8:57 am 
Newbie

Joined: Fri Jul 30, 2010 8:39 am
Posts: 1
Hello,

I have quite complex database model, an Ad has a collection of Details, every Detail has reference to Parameter, a Parameter belongs to a Group and to a Field.
A Field has OptionItems and Units.
OptionItem, Unit and a Group have Translations collection.
There are more relations, but these mentioned above are the most relevant.

I need to get efficiently full object graph for specific Ad. My solution gets it in one query, but there are too many joins and I get a cartesian product.

My query:
Code:
var ad = _session.CreateCriteria<Ad>("ad")
                //Address with Country and Country Translations
                .CreateAlias("Address", "address", JoinType.LeftOuterJoin)
                .SetFetchMode("address", FetchMode.Eager)
                .CreateAlias("address.Country", "country", JoinType.LeftOuterJoin)
                .SetFetchMode("country", FetchMode.Eager)
                .CreateAlias("country.Translations", "countryTranslations", JoinType.LeftOuterJoin)
                .SetFetchMode("countryTranslations", FetchMode.Eager)
                //Details
                .CreateAlias("Details", "details", JoinType.LeftOuterJoin)
                .SetFetchMode("details", FetchMode.Eager)
                //Parameter
                .CreateAlias("details.Parameter", "parameter", JoinType.LeftOuterJoin)
                .SetFetchMode("parameter", FetchMode.Eager)
                //Group with Translations
                .CreateAlias("parameter.Group", "group", JoinType.LeftOuterJoin)
                .SetFetchMode("group", FetchMode.Eager)
                .CreateAlias("group.Translations", "groupTranslations", JoinType.LeftOuterJoin)
                .SetFetchMode("groupTranslations", FetchMode.Eager)
                //Field
                .CreateAlias("parameter.Field", "field", JoinType.LeftOuterJoin)
                .SetFetchMode("field", FetchMode.Eager)
                .CreateAlias("field.Translations", "fieldTranslations", JoinType.LeftOuterJoin)
                .SetFetchMode("fieldTranslations", FetchMode.Eager)
                //OptionItems with translations
                .CreateAlias("field.OptionItems", "optionItems", JoinType.LeftOuterJoin)
                .SetFetchMode("optionItems", FetchMode.Eager)
                .CreateAlias("optionItems.Translations", "optionItemTranslations", JoinType.LeftOuterJoin)
                .SetFetchMode("optionItemTranslations", FetchMode.Eager)
                //Units with translations
                .CreateAlias("field.Units", "unit", JoinType.LeftOuterJoin)
                .SetFetchMode("unit", FetchMode.Eager)
                .CreateAlias("unit.Translations", "unitTranslations", JoinType.LeftOuterJoin)
                .SetFetchMode("unitTranslations", FetchMode.Eager)
                .CreateAlias("Images", "images", JoinType.LeftOuterJoin)
                .SetFetchMode("images", FetchMode.Eager)
                .CreateAlias("Make", "make", JoinType.LeftOuterJoin)
                .SetFetchMode("make", FetchMode.Eager)
                .CreateAlias("make.Models", "model", JoinType.LeftOuterJoin)
                .SetFetchMode("model", FetchMode.Eager)
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .Add(Restrictions.Eq("Id", id))
                .UniqueResult<Ad>();


As you can see it gets huge... And not really efficient.
Any ideas how to optimize it using NHibernate?

Thanks in advance.
Marek


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.