in my application i´m using SQL CE 4.0 and Entity Framework Code First.
public class Customer : IEntityPoco
{
public int Id { get; set; }
public string Sex { get; set; }
public string Titel { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string Fax { get; set; }
public string Notice { get; set; }
public DateTime LastChange { get; set; }
public int AddressId { get; set; }
public Address Address { get; set; }
public ICollection<Sale> Sales { get; set; }
public ICollection<Customer> Partner1 { get; set; }
public ICollection<Customer> Partner2 { get; set; }
public ICollection<Phone> PhoneNumbers { get; set; }
public ICollection<Email> EmailAddresses { get; set; }
public ICollection<Corrospondence> Corrospondence { get; set; }
public ICollection<ShippingAddress> ShippingAddresses { get; set; }
}
thats what my model looks like,
var query = context.TeacherCustomers
.Include(i => i.EmailAddresses)
.Include(i => i.Corrospondence)
.Include(i => i.Address.City.Country)
.Include(i => i.ShippingAddresses)
.Include(i => i.Sales.Select(f => f.BuildVersion.Product))
.Include(i => i.PhoneNumbers)
.Include(i => i.Partner1.Select(f => f.Address.City.Country))
.Include(i => i.Partner2.Select(f => f.Address.City.Country))
.AsNoTracking();
and that´s what my query look´s like. In my Business Logic i need all Email Addresses, Phone Numbers, … If i comment all Include Statements this query is executed in about 82ms, if all include statements are not commented the query takes about 52000ms (and it is not the first query so the database model is already compiled).
This post here explains the problem very good: How many Include I can use on ObjectSet in EntityFramework to retain performance?, but the solution given there is also not an option for me because my Database contains more then 10 000 entries and when i execute the query and then stepping through all customers returned by the query and calling context.Entry(customer).Reference(i => i.EmailAddresses).Load() takes also a lot of time.
So how can i create a query that has the same result as the query above but is executed faster?
help would be appreciated
I am not sure if you will be able to get much performance out of this model. You are essentially cross joining all the tables, which will give you a huge table and a lot of records.
I suggest to rethink your model, may be denormalize a bit, do you really need tables for emails, and phone numbers cant they be just multiple columns in the table (most apps do not need more than 2 mails or phone numbers).
If there is nothing you can do about the model, I suggest load all tables separately like use one query to load all Customers, and one to load all emails and so forth then join them in the model. based on the statistics you have provided it will take you less than 400 ms to load all the data.