So, I've got a fairly large relational database that I'm driving with EntityFramework in C#/.Net. I'm trying to optimize it to make it better at getting only select data.
I am using .AsNoTracking()
as a bool
input to the repository before trying to limit any data.
Up until now, I've been using repositories where All of the related tables are included in the query, even when I only want to search for certain things.
I've basically got a massive list of .Include(...)
before returning an IQueryable<Product>
.
But say I only want to return a couple of particular tables(as those are the only ones I need to display certain data), how is the best way of doing this?
So far, I've got my repository taking in a list of enum values for the relations available to that entity.
I've implemented looping through this list, and trying to do a .Include(...)
on each required relation, but this appears to take ALOT longer than loading everything(sometimes, even timing out and giving me a SQL timeout). A short snippet of this is below.
query
is an IQueryable<Product>
foreach (var part in this.IncludeList)
{
switch(part)
{
case ProductPart.Tag:
query = query.Include(p => p.Tags);
break;
case ProductPart.Video:
query = query.Include(p => p.Videos);
break;
}
}
Is there a knack to using this out of chain that I'm missing?