开发者

Entity Framework projection into class is faster than selecting EF POCO objects, WHY?

This one confounds me.

I am using EF 4.1 and have applied a T4 template to the model to use POCO's and POCO proxies (public paramaterless ctor, all properties are virtual, all navigation properties are ICollection).

I have a table with around 1.1M records in it. I was doing some timing tests to see how long it takes to retrieve this data and got some unexpected results.

This call returns in about 21 seconds:

ctx.Valuations.MergeOption = MergeOption.NoTracking
var entityValuations = ctx.Valuations.OfType<Foo>().ToArray();

This call takes 9 seconds:

ctx.Valuations.MergeOption = MergeOption.NoTracking
var entityValuations = ctx.Valuations.OfType<Foo>().Select(v => new Val()
{
    ID = v.ID
    ...
    //set all properties
    ....
}).ToArray();

The only difference between thes开发者_StackOverflowe two statements is that the first returns EF poco proxies and the second projects the result set into a non-proxy poco. That is such a HUGE difference in performance time! I am completely stumped as to why and cannot come up with any legitimate reason.

Does anyone know why the second statement is so much faster?


POCO proxies are just that - proxies. A new proxy class must be created for every one and added to the context. Although you have essentially said 'dont track them in the context' Id think it would be a bit faster, but I think you are creating an additional 1.1M objects that are proxies.

Note from: http://msdn.microsoft.com/en-us/library/dd456853.aspx

" You can have a mix of POCO entities and proxy entity objects. To disable creating proxy objects, set the value of the ProxyCreationEnabled property to false on the instance of ObjectContextOptions that is returned by the ContextOptions property on the ObjectContext: "

It would be an interesting test to turn off the proxy on the context via ProxyCreationEnabled and see what your result is - I'd gather similar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜