Slow EntityCollection.AddRange when using Context

Posts   
 
    
Rosacek
User
Posts: 155
Joined: 18-Mar-2012
# Posted on: 20-Mar-2016 21:21:04   

I have latest LLBL, using adapter.

I found issue with EC.AddRange performance when EC is in Context

Scenario: Say I want "refresh" ecManagedCollection First I fetch entities into ecTempCollection and if some conditions are ok, then I want to put those entities to ecManagedCollection.

Very simple code looks like:

Dim myContext As New Context()
myContext.Add(ecManagedCollection)

Using adapter As New DataAccessAdapter()
    Dim ecTempCollection As New HelperClasses.EntityCollection(Of myEntityType)()
    adapter.FetchEntityCollection(ecTempCollection)

    ecManagedCollection.Clear()
    ecManagedCollection.AddRange(ecTempCollection)
End Using

If I fetch 10k records then command "ecManagedCollection.AddRange(ecTempCollection)" takes about 10-20seconds.

If I disable using context by comment out "myContext.Add(ecManagedCollection)" then fetching 10k records takes 0,5sec

If I use Context but I do not use ecTempCollection, I fetch directly to ecTempCollection, then it takes also 0,5 sec.

Means looks like using Context brings huge overhead into .AddRange. Could you look at that?

I tried in different apps, different databases and different DAL code. Same result, when EC is in Context then EC.AddRange is 10-20x slower that direct fetch or when using ecTemp+not using context.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Mar-2016 05:53:05   

What is your LLBLGen version and runtime library version? ( http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7717 )

What are the special checks that you do before adding entities from ecTempCollection to ecManagedCollection?

David Elizondo | LLBLGen Support Team
Rosacek
User
Posts: 155
Joined: 18-Mar-2012
# Posted on: 21-Mar-2016 09:56:24   

LLBLGEN v4.2.16.311

Even in posted simple code without any additional logic the .AddRange is very slow

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 21-Mar-2016 15:31:30   

This is likely due to events being fired, as adding an entity to a collection means the FK fields are synced too.

It's also the case that adding an entity will do a linear search for checking whether the same entity already exists. If you know the data is new (like in your case) set the DoNotPerformAddIfPresent property of the collection to false. This way the collection won't make a linear search each time an entity is added, which should speed things up.

Frans Bouma | Lead developer LLBLGen Pro