I have a data model made up of several entities, all of them interlinked, all in all about 80 tables.
I'm starting to get the code in production shape, and in the process I am switching from lazy loading everything (it made my development life easier) to eager loading in cases where I don't need all the related data, or it simply represents too much data to be sent to the client, it's just a waste of bandwidth.
However, I just noticed that, for at least some entities, eager loading just what I need is dramatically slower than lazy loading all of the data.
Here is an example, I can load a "request" object either lazily:
db.ContextOptions.LazyLoadingEnabled = true;
request = db.requests.Where(rq => rq.idrequest == rID).FirstOrDefault();
or eagerly:
db.ContextOptions.LazyLoadingEnabled = false;
request = db.request_objects.Include("service_objects.task_objects.task_parameters.parameter_values.address1").Include("service_objects.task_objects.task_parameters.parameter_values.attachment").
Include("service_objects.task_objects.task_parameters.parameter_values.phone_nbrs").Include("service_objects.task_objects.task_parameters.parameter_values.stored_texts").
Include("service_objects.task_objects.parent_tasks").Include("service_objects.task_objects.contact_objects").
Include("service_objects.service_parameters.parameter_values.address1").Include("service_objects.service_parameters.parameter_values.attachment").
Include("service_objects.service_parameters.parameter_values.phone_nbrs").Include("service_objects.service_parameters.parameter_values.stored_texts").
Include("service_objects.stored_texts").
Include("request_attachments.attachment").Include("request_notes.note").
Include("request_contacts.contact_objects").Include("contact_objects").
Include("contact_objects1").Include("contact_objects2").
Include("request_objects_links.request_objects1").Include("stored_texts").
Include("company_objects").
Where(ro => ro.idrequest_objects == rID).FirstOrDefault();
In most cases, eager loading the request is hundreds or thousands of times (!!!) slower than lazily loading it, although more often than not lazy loading will load A LOT of extra data. I mean, using eager loading it takes between 2 and 3 seconds, whereas it takes less than 40 ms in most cases (often less than 10 ms) with lazy loading (I got those times using a System.diagnostics.Stopwatch).
I am no SQL expert, and don't know anything about SQL optimization, but I can't quite wrap my head around why loading less data is more expensive.
Any insight, or obvious mistake on my part? Thanks!
EDIT
Judging from Brokenglass's answer, I wasn't clear enough :o). This code is just part of a WCF service function, pretty straightforward:
[OperationContract]
public request LoadRequestByID(int rID)
{
request res = null;
try
{
DBEntities db = new DBEntities();
res = db.request_objects.Where(ro => ro.idrequest_objects == rID).FirstOrDefault();
}
catch (Exception e)
{
//Error log
}
return res;
}
I decided to time it when I noticed that it took considerably longer to display a request's details on the client end (after calling the above function) when eager-loading as opposed to lazy-loading.