-->
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: Reducing DB-Traffic on Queries?
PostPosted: Thu Oct 02, 2008 3:55 am 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
Hi,

I have a n:m association of two classes (Project and Line). I created the following query:

Code:
DetachedCriteria matchingProjects = DetachedCriteria.For(typeof (Project))
                .CreateCriteria("Linen", "projectLinen")
                .Add(Subqueries.PropertyIn("projectLinen.LineId", lineOfUser));

            IList<Project> projekts = matchingProjects.GetExecutableCriteria(session)
                .List<Project>();


But I noticed while studying the SQL send to the server, that all columns of Project as well as all columns of Line are being retrieved from the server. For one, the columns of Line are actually not needed (that's why I made the association of lines to products lazy! :) ) and second this gives me way too many results in my list.

I managed to narrow the list returned by the query by adding

Code:
.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())


but this still fetches way to many data from the DB, which is totally unnecessary.

Speaking of unnecessary "DB-noise" ... since Project and Line have a n:m relation there exists a db-table 'ProjectLine', which holds the primary keys of Project and Line. When I add

Code:
.CreateCriteria("Linen", "projectLinen")


a join from Project->ProjectLine->Line is actually performed. But since I'm just filtering on the primary key of Line, the last join to the Line-table is actually not necessary, because this information could also be retrieved from the ProjectLine-table - but how can I tell the criteria to do such an "optimization"?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 9:59 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What happens if you do it this way:
Code:
DetachedCriteria matchingProjects = DetachedCriteria.For(typeof (Project),"p")
                .Add(Subqueries.PropertyIn("p.Linen.LineId", lineOfUser));

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 12:38 pm 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
wolli wrote:
What happens if you do it this way:
Code:
DetachedCriteria matchingProjects = DetachedCriteria.For(typeof (Project),"p")
                .Add(Subqueries.PropertyIn("p.Linen.LineId", lineOfUser));


well ... like in the other thread at http://forum.hibernate.org/viewtopic.php?t=991142 I changed the property-name from 'LineId' back to 'Id' ... so I get this:

Code:
NHibernate.QueryException: could not resolve property: Lines.Id of: NHibernateQueries.Project


:(

BTW: misspelled something there ... the collection-property is called 'Lines' not 'Linen' :)


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.