entity framework - Loading most related entities of an object is very slow -
continuing earlier question described schema (repeated here convenience):
parties ( partyid, clientid, addressid, datetime ) tents ( partyid, tentdetails... ) clowns ( partyid, addressid, clowndetails... ) securityagentassignment ( partyid, agentid, fromtime, untiltime ) addresses ( addressid, street, city, state )
....and there's 10 other tables of similar design: in many-to-one relationship parties
.
my asp.net mvc web application has summary page displays every detail party. i'm using ef1.0 have own eager-loading. here's logic i'm using:
party dbparty = getparty(partyid); dbparty.tents.ensureloaded(); dbparty.clowns.ensureloaded(); foreach(clown clown in dbparty.clowns) clown.address.ensureloaded(); dbparty.security.ensureloaded(); foreach(securityagentassignment assignment in dbparty.security) assignment.agent.ensureloaded(); // , 10 other relationships
the code above takes 3 seconds run. given isn't eager-loading, lazy-loading, surely should fire off 15 simple select queries , done?
i don't have sql server profiler installed, , don't know how sql generated when you're using .load
instead of iqueryable
.
i use these extension methods helpers:
private static readonly r.fieldinfo _entityreferencecontext = typeof(relatedend).getfield("_context", r.bindingflags.instance | r.bindingflags.nonpublic ); private static readonly r.propertyinfo _relatedendowner = typeof(relatedend).getproperty("owner", r.bindingflags.instance | r.bindingflags.nonpublic ); private static boolean isattached(this relatedend relatedend) { object context = _entityreferencecontext.getvalue( relatedend ); return context != null; } public static tentity ensureloaded<tentity>(this entityreference<tentity> eref) tentity : class, ientitywithrelationships { // entityreference<tentity> derives relatedend. relatedend erefasrelatedend = (relatedend)eref; erefasrelatedend.ensureloaded(); return eref.value; } public static void ensureloaded(this relatedend end) { ientitywithrelationships owner = (ientitywithrelationships)_relatedendowner.getvalue( end, null ); entityobject ownerentity = owner entityobject; if( ownerentity != null ) { if( ownerentity.entitystate == entitystate.added || ownerentity.entitystate == entitystate.detached ) return; // calling .load on added object causes exception. } if( end.isattached() && !end.isloaded ) end.load(); }
this question moot - connecting database server on vpn connection. thought okay ping time 50ms between computer , database server, forget how chatty sql server is. considering fires off 60 queries @ once makes sense 60 * 50ms == 3000ms
.
when retried application on same lan server (ping time < 1ms) whole load operation executes in under 30ms no need further optimization. problem solved.
Comments
Post a Comment