How to speed up access to lists?

2 posts, 0 answers
  1. Wolfgang
    Wolfgang avatar
    98 posts
    Member since:
    Oct 2012

    Posted 13 Nov 2013 Link to this post

    Hi 

    we have the following

    objectA is referenced in objectB
    objectB has a IList<TypeA>

    We want to remove A from B.

    Therefore we used:

    If(B.IListOfTypeA != null)
        B.IlistOfTypeA.Remove(objectA);

    Which seems to be quite simple.

    Now we have the following problem: speed.

    We have 4500 objects in the list.
    If we try to look wether object B has a list ( If(B.IListOfTypeA != null) ) this command needs over a second.

    Digging deeper and using the OpenAccess.Profiler in the profiler is shown that :
    Navigation ....TypeB(Key=...).IListOfA takes up the complete time (2 selects with e.g. 993 and 653 ms are shown).

    Is there any way to speed it up?

    Best regards,
    Wolfgang


  2. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 15 Nov 2013 Link to this post

    Hi Wolfgang,

    The two operations that you are seeing are - one select operation to load the IListTypeA and another to update the foreign key. You can avoid the list loading if you change a bit your code:
    1. Set IsManaged to false for IlistOfTypeA navigation property.
    2. Rewrite the removal code the other way around - objectA.ParentB = null or even objectA.ParentBId = null.
    Unfortunately these changes will have some side effects:
    • IsManaged = False means that OpenAccess will not synchronise your navigation properties when you set them and you will need to explicitly add the related objects to the OpenAccess context. This will apply globally so pay attention.
    • Setting directly the foreign key Id will in combination with IsManaged = False will lead to incorrect values in both A.IListOfTypeA and B.ParentB properties - they will contains the old referenced object until you call context.SaveChanges();
    • Setting directly B.ParentB property will not update the foreign key property and A.IListOfTypeA property until context.SaveChanges() is called;
    Using the described approach allowed me to cut the required time in half, but I cannot guarantee that you will experience the same performance boost.

    Please share your results with us if you decide to apply the suggested optimizations.

    Regards,
    Viktor Zhivkov
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  3. DevCraft banner
Back to Top