c# - Entity Framework Performance - How do I get this to work fast? -
i have object has list of child objects, institution has multiple departments. departments can disabled or enabled based on boolean property on department. trigger property of institution false when there 1 or more departments disabled. have following code in partial institution class:
public bool allset { { return !(departments.where(i => i.active == false && i.isdeleted == false).count() > 0); } }
this works, when have enabled performance of page shows list of institutions slows crawl , memory usage spikes, apparently doing fundamentally wrong here, have alternative ways this?
working solution
var institutions = x in ent.institutions let hasdepartments = !x.departments.any(d => d.active == false && d.isdeleted == false) select new { fulltitle = x.title + " - " + x.address.line1 + ", " + x.address.city + ", " + x.address.state, department = x, allset = hasdepartments, guid = x.guid }; instlist.datasource = institutions;
you should find marginal performance benefit using following query. main issue linq unperformant. definition generating sql query @ run time , in order large amount of meta-data generated. find .net 4.5 has lower overheads .net 4 in regards.
if there seems large cost first query, due view creation process. can take @ pre-compiled views. if need speed, elsewhere. perhaps inline sql.
public bool allset { { return !(departments.any(i => i.active == false && i.isdeleted == false)); } }
edit: realised real issue. looks allset
method on class institution , calling on tight loop. assume "working" because of lazy loading.
this hugely bad each loop going making database call, going io/latency bound , hence extremely slow. instead of loading bool
database, each , every associated department
loaded on demand , .count()
run locally on .net (hence large memory footprint).
try prefetch each institution along if have active departments.
var institutions = x in context.institutions {blah} let hasdepartments = x.departments.any(d => d.active == false && i.isdeleted == false) select new { department = x, allset = hasdepartments}; foreach(var institution in institutions) { //do stuff }
overall real wtf relying on ef lazy loading.
Comments
Post a Comment