实体框架投射到类是比选择EF POCO对象更快,为什么呢?更快、什么呢、实体、框架

2023-09-06 05:37:57 作者:疗伤歌手。

这其中混淆了我。

我使用的EF 4.1和应用了T4模板模型使用POCO的POCO和代理(公共paramaterless构造函数,所有属性都是虚拟的,所有的导航属性ICollection的)。

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).

我在它周围110万记录的表。我在做一些计时测试,看看需要多长时间来获取这些数据,并得到了一些意想不到的结果。

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.

在约21秒此调用返回的:

This call returns in about 21 seconds:

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

此调用需要9秒时:

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

这两个语句之间的唯一差别是,第一个返回EF POCO代理和所述第二项目结果集成非代理波苏。这是表演时间如此巨大的差异!我彻底难倒,为什么并不能拿出任何正当的理由。

The only difference between these 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代理是 - 代理。一种新的代理类必须为每一位被创建并添加到上下文。虽然你已经基本上说:不要跟踪他们的背景下,标识认为这将是一个有点快,但我认为你正在创建的代理额外的1.1M的对象。

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.

请注意来自: http://msdn.microsoft.com/en-us /library/dd456853.aspx

你可以有POCO实体和代理实体对象的组合。要禁用创建代理对象,设置ProxyCreationEnabled属性设置为false上是通过在ObjectContext中的ContextOptions属性返回ObjectContextOptions的实例的值:

" 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: "

这将是一个有趣的测试,关闭经由ProxyCreationEnabled的背景下,代理,看看你的结果是 - 我想收集类似

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.

 
精彩推荐