Are you lazy loading your way to performance problems?

Dinesh recent post, Lazy ORM users deserve it!,  talks about how lazy loading can lead to performance and concurrency problems.  I’m slowly coming to the conclusion that lazy loading is the root of all that people see as evil when it comes to O/R Mappers.  By hiding the loading of objects from a data store behind a lazy loaded property we make the cost hidden to the end user who is utilizing our objects.  It might be known that lazy loading leads to additional database hits, however, often times the scope and depth of database interaction that can result from lazy loading is not fully realized.

Many O/R Mappers support eager fetching of related objects to help reduce the amount of database interaction that is required for loading objects which are known to be required for a particular set of domain logic (See Ayende Combating the SELECT N + 1 Problem).  In additional to supporting eager fetching some mappers have a caching layer that helps reduce the amount of database interaction necessary for loading the data you need.  Often times it isn’t enough to prevent an innocent looking set of domain logic from causing an explosion of database hits.

So the question is...do we need to get better at using our O/R Mappers efficiently, or should our O/R Mappers make us be explicit about what we want them to retrieve and do away with lazy loading?  What’s your take?

* By the way this problem isn’t unique to O/R Mappers

# re: Are you lazy load your way to performance problems?

Thursday, May 18, 2006 9:08 AM by Steve    
Thanks for the link Alex, I've been meaning to take another look at Base4 so perhaps this will give me a good excuse to dig into the details. As you mentioned having these details in the mapping file isn't sufficient since it really needs to be on a query by query basis.


# re: Are you lazy load your way to performance problems?

Thursday, May 18, 2006 9:17 AM by Steve    
Ayende, I agree that anyone who uses an O/R Mapper should understand how it works and be cognizant of the effect that their code will have on their database. However, it seems that a lot of people get into trouble with O/R Mappers because they don't realize the effect of lazy loading.

When I mentioned making it be more explicit I meant when the query/criteria is constructed for the data it should explicitly say what data should be loaded. Then if the code tried to access data that wasn't explicitly loaded it wouldn't be there.

I think a lot of times O/R Mappers get a bad rap because of the problems (such as this) that they can introduce. I'm wondering if there is something we can do to help/prevent such things.

# re: Are you lazy loading your way to performance problems?

Thursday, September 28, 2006 7:09 AM by Jim    
Many people neglect that exact point indeed. Performance is taken often as something that may solve itself with time, or it's just temporary issue.

Post a Comment

 
 
Prove you're not a spammer: 
0 + 1 =