How to speed up access to lists?

Thread is closed for posting
2 posts, 0 answers
  1. Wolfgang
    Wolfgang avatar
    98 posts
    Member since:
    Oct 2012

    Posted 13 Nov 2013 Link to this post


    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)

    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,

  2. Viktor Zhivkov
    Viktor Zhivkov avatar
    324 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.

    Viktor Zhivkov
    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!
Back to Top