none
Which method has a better performance ? RRS feed

  • Question

  • Hello !

    I'm using entity framework , and I have several cases where I should run a query than return some parent items , and after I display these parents and the related children  in one report.

    I want to know which of these methods have the better performance : ( or is there any other better method ??? )

    Method1: (the childs collection are loaded later , using lazy loading)
    Dim lista as IQueryable(Of MyObj) = (From t In context.MyObjs Where(..condition..) select t).Tolist 
    
    
    Method2:
    Dim lista as IQueryable(Of MyObj) = (From t In context.MyObjs Where(..condition..) _
                .Include(Function(t2) t2.Childs1) _
                 .Include(Function(t2) t2.Childs2) _
                 .Include(Function(t2) t2.Childs2.Child22) _
                  .Include(Function(t2) t2.Childs1.Childs11) _
                  .Include(Function(t2) t2.Childs1.Childs12) _
                  Select t).ToList
    
    Method3:
    Dim lista as IQueryable(Of MyObj)
    Dim lst= (From t2 In context.MyObjs Where(..condition..) Select New with _
           {    .Parent=t2
                .ch1=t2.Childs1 _
                 .ch2=t2.Childs2 _
                 .ch21=t2.Childs2.Child21) _
                 .ch11=t2.Childs1.Childs11) _
                  .ch12= t2.Childs1.Childs12 _
                  ).ToList
     lista=lst.Select(Function(t2) t2.parent)
    

    I noticed that the first method cause the report to open very slow. Also I read somewhere that Include() cause repeat of parent items?

    But anyway I want a professional opinion in general for the three methods.

    Thank you !


    • Edited by dcode25 Thursday, February 12, 2015 2:08 AM
    Thursday, February 12, 2015 2:07 AM

Answers

  • Hello,

    >> I don't know the logic when use Projections like in the third mode ? Are the master rows duplicated ?????

    If you use code as to trace the generated sql statement, you could(at least in my machine) the second and third are same which will use a Join syntax, and so that , the master table records are actually duplicated.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 7:26 AM
    Moderator

All replies

  • Hello,

    As far as I know, the Entity Framework offers two ways to load related data after the fact. The first is called lazy loading and, with the appropriate settings, it happens automatically. In your case, your first method uses the last loading, while the second and third are the same actually, both of them are Eager Loading. (In VB, if you could check use code as “DbContext.Database.Log = Sub(val) Diagnostics.Trace.WriteLine(val)” to see the actually generated sql statement, you could see the third and second query would generate a join syntax). Since you mentions, the lazy loading way is low performance, you could use either the second or third one.

    >>Also I read somewhere that Include() cause repeat of parent items?

    For this, nor sure if you worry it would firstly use lazy loading and then use eager loading, however, in my test, I do not see this behavior, the Entity Framework seems to be smart enough to use one mode to load data at the same time. Or you could disable its lazy loading when using eager loading:

    context.ContextOptions.LazyLoadingEnabled = false

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 13, 2015 3:07 AM
    Moderator
  • Hello !

    according to this link :

    http://stackoverflow.com/questions/5521749/how-many-include-i-can-use-on-objectset-in-entityframework-to-retain-performance

    -----------------------------------------------------------------

    For example: Master -> Details.  Say, master has 100 rows, Details has 5000 rows (50 for each master).

    If you lazy-load the details, you return 100 rows (size: master) + 5000 rows (size: details).

    If you use .Include("Details"), you return 5000 rows (size: master + details).  Essentially, the master portion is duplicated over 50 times.

    -------------------------------------------------------------------

    I don't know the logic when use Projections like in the third mode ? Are the master rows duplicated ?????

    Friday, February 13, 2015 4:13 AM
  • Any response ?

    Thank you !

    Wednesday, February 18, 2015 2:30 AM
  • Hello,

    >> I don't know the logic when use Projections like in the third mode ? Are the master rows duplicated ?????

    If you use code as to trace the generated sql statement, you could(at least in my machine) the second and third are same which will use a Join syntax, and so that , the master table records are actually duplicated.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 7:26 AM
    Moderator