简体   繁体   English

我可以避免实体框架6中复杂类型的性能问题吗?

[英]Can I avoid performance problems with complex types in entity framework 6

I have a complex type 我有一个复杂的类型

[ComplexType]
public class mm 
{ 
    public Guid g { get; set; }
}

and an inheritance hierarchy 和继承层次

public abstract class base1
{
    public Guid id { get; set; }
    public String name { get; set; }
    public mm mm1 { get; set; }
}

and various derived classes: 以及各种派生类:

public class derived1 : base1
{
    public mm derived1mm1 { get; set; }
}

The derived classes have multiple mm instances in them, so that with a hierarchy of approximately 10 derived classes there are a total of about 100 mm fields. 派生类中有多个mm实例,因此在大约10个派生类的层次结构中,总共有大约100 mm字段。 The resulting database schema is correct and looks roughly like this: table base1 column id (guid) column name (string) column mm1_g ( guid) column derived1mm1_g (guid) column derived1mm2_g (guid) column derived2mm1_g (guid) ...... 生成的数据库架构是正确的,并且大致如下所示:表base1列id(guid)列名(string)列mm1_g(guid)列导出1mm1_g(guid)列派生1mm2_g(guid)列派生2mm1_g(guid)......

On startup, the first query in EF ( the one which warms things up ) takes tens of seconds. 在启动时,EF中的第一个查询(预热查询)需要数十秒钟。

If I replace the mm complex type with individual fields corresponding to it so the resulting classes look like this: public abstract class base1 { public Guid id { get; 如果我用与之对应的单个字段替换mm复杂类型,则结果类如下所示:public abstract class base1 {public Guid id {get; set; 组; } public String name { get; } public String name {get; set; 组; } public guid mm1 { get; } public gui mm1 {get; set; 组; } } }}

and various derived classes: 以及各种派生类:

public class derived1 : base1
{
    public guid derived1mm1 { get; set; }
}

, the startup time is sub one second. ,启动时间不到一秒。 The only difference is that I've flattened out the complex type, which is not a good answer if the complex type has multiple fields and its own behaviour. 唯一的区别是我已经弄平了复杂类型,如果复杂类型具有多个字段并且具有自己的行为,那么这不是一个很好的答案。

Just to be clear, this performance hit happens only on the very first query, and happens even if there is absolutely no data in the database. 需要明确的是,这种性能下降只会在第一次查询时发生,即使数据库中绝对没有数据也会发生。

mm is a complex type, not an entity in its own right and there are no tables created for it, so there are no joins involved or anything. mm是一种复杂类型,本身并不是一个实体,并且没有为其创建任何表,因此不涉及任何联接。

This sounds like a bug of some kind in EF6's model generation implementation where large numbers of complex type fields are involved. 这听起来像是EF6的模型生成实现中的某种错误,其中涉及大量复杂类型字段。

Does anyone have a workaround which will allow me to retain the complex type. 是否有人有变通办法,使我可以保留复杂类型。 Is this a known bug? 这是一个已知的错误?

I've recently done some Entity performance improvements on an eCommerce application. 我最近在电子商务应用程序上做了一些实体性能改进。

Somethings we learned, that might help you 我们学到的东西可能对您有帮助

  • Use your .Include(mm => mm.derivedmm) as late as possible, just before your executing call (eg .ToList()) 在执行调用之前(例如.ToList()),尽可能晚使用.Include(mm => mm.derivedmm)
  • Ask yourself, if you need all that data at once. 问问自己,是否需要一次所有这些数据。 It might be better just to load the list of id's if you dont need the data. 如果您不需要数据,最好只加载ID列表。 (Lazy loading) (延迟加载)
  • Run SQL profiler and have a look a the SQL entities is executing. 运行SQL事件探查器,看看SQL实体正在执行。 Sometimes simply reordering your Linq queries can really optimize the scripts it generates. 有时,只需对Linq查询重新排序就可以真正优化其生成的脚本。
  • Put data in the ASP.NET cache once its loaded. 加载数据后,将数据放入ASP.NET缓存中。 We put HEAPS of data in the memory cache and the performance improvement is unbelievable. 我们将HEAPS数据放入内存缓存中,其性能令人难以置信。 Your database is a bottleneck, accessing data from memory is 1000x faster. 您的数据库是一个瓶颈,从内存访问数据的速度要快1000倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM