Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid large generated SQL queries in EF when using Include()

I'm using EF (dll version is 4.4) to query against a database. The database contains several tables with course information. When having a look what actually is sent to the db I see a massive, almost 1300 line SQL query (which I'm not going to paste here because of it's size). The query I'm running on the context looks like:

entities.Plans
  .Include("program")
  .Include("program.offers")
  .Include("program.fees")
  .Include("program.intakes")
  .Include("program.requirements")
  .Include("program.codes")
  .Include("focuses")
  .Include("codes")
  .Include("exceptions")
  .Include("requirements")
where plans.Code == planCode
select plans).SingleOrDefault(); 

I want to avoid having to go back to the server when collecting information from each of the related tables but with such a large query I'm wondering if there is there a better way of doing this?

Thanks.

like image 208
b3n Avatar asked Mar 21 '13 05:03

b3n


1 Answers

A bit late but, as your data is only changing once a day, look at putting everything you need into an indexed view and place this view in your EF model.

like image 157
A Aiston Avatar answered Nov 10 '22 01:11

A Aiston