-->
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.  [ 4 posts ] 
Author Message
 Post subject: Performance of lazy collection count
PostPosted: Tue Oct 09, 2007 3:40 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
Recently I have started profiling my application and found that there is a great performance hit when I am trying to get Count of elements of a lazy collection. For example if I had such mapping:

Code:
<class name="Foo" lazy="false">
    <bag name="BarList" table="Bar" inverse="true" cascade="all-delete-orphan" lazy="true" batch-size="100">
      <key column="FooId"></key>
      <one-to-many class="Bar"/>
    </bag>
</class>


And tries to execute code like:

Code:
    int elementsCount = Foo.BarList.Count;


Then the profile data shows me that it takes about 180 ms to execute. There is no doubt that NHibernate tries to get the data from the database and after that it gets count of elements.

Is there a way to optimize this behavior? I don't want to write custom queries just to get a count of elements in a collection. Is there is a way to instruct NHibernate to load all element Ids even when using lazy load? So it would look like the collection already has all elements (which are actually proxies, but contains real object ids) and calling Count didn't force database query?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 09, 2007 6:24 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
There's a trick for this explained in the documentation in section 15.1.4: http://www.hibernate.org/hib_docs/nhibe ... ialization

Essentially it says you can use the following:

Code:
(int) s.CreateFilter( collection, "select count(*)" ).List()[0]


Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 09, 2007 7:05 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
merge_s.rottem wrote:
There's a trick for this explained in the documentation in section 15.1.4: http://www.hibernate.org/hib_docs/nhibe ... ialization


Thanks for the answer, it helps a bit, but not entirely what I want. I can't use this solution anywhere, because in that case internal implementation of data access layer become obvious to any other layer (NHibernate is used). What I want is to make calling Count property absolutely transparent, but still reduce execution time.


Top
 Profile  
 
 Post subject: Re: Performance of lazy collection count
PostPosted: Wed Oct 10, 2007 4:35 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
Ishitori wrote:
Is there is a way to instruct NHibernate to load all element Ids even when using lazy load? So it would look like the collection already has all elements (which are actually proxies, but contains real object ids) and calling Count didn't force database query?

Actually, I thought 'extra-lazy' could do that that but I'm pretty sure that NHibernate doesn't it implement it anyway ;-(

From the 'Hibernate' doco:
Quote:
Use lazy="extra" on collections for "smart" collection behavior, i.e. some collection operations such as size(), contains(), get(), etc. do not trigger collection initialization. This is only sensible for very large collections.


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