bilkusg
bilkusg

Reputation: 126

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

On startup, the first query in EF ( the one which warms things up ) takes tens of seconds.

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; set; } public String name { get; set; } public guid 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.

This sounds like a bug of some kind in EF6's model generation implementation where large numbers of complex type fields are involved.

Does anyone have a workaround which will allow me to retain the complex type. Is this a known bug?

Upvotes: 0

Views: 523

Answers (1)

jennas
jennas

Reputation: 2464

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())
  • 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. (Lazy loading)
  • Run SQL profiler and have a look a the SQL entities is executing. Sometimes simply reordering your Linq queries can really optimize the scripts it generates.
  • Put data in the ASP.NET cache once its loaded. We put HEAPS of data in the memory cache and the performance improvement is unbelievable. Your database is a bottleneck, accessing data from memory is 1000x faster.

Upvotes: 1

Related Questions