Fetch all entities

Posts   
 
    
pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 15-Apr-2012 13:53:22   

I have an unusual task for working with the DB: I need to read-in all data from db into memory at once, no lazy fetching. Does llbgen has support for such fetch? If no how do you suggest to proceed with this task in order to achieve best performance?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Apr-2012 07:00:14   

From your previous threads I assume you are using Adapter. If your DB is full connected (i.e., you can reach any table B from any table A through relations) you can use PrefetchPaths to fetch the full graph.

You also can fetch all collection individually, by passing null in the filter parameter at adapter.FetchEntityCollection(...).

So, of course it's doable, but depending on your DB size there are changes that you will run into out-of-memory problems.

David Elizondo | LLBLGen Support Team
pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 16-Apr-2012 14:00:05   

Yes I am using adapter mode. if I decide to use adapter.FetchEntityCollection with no prefetch paths, but provide Context, will Context link the entities or will I need to do it myself?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Apr-2012 18:02:04   

Context won't link entities or asociate them together, the Context is used to make sure you don't get more than one instance of the same entity.

If you want a connected graph, you'd better use PrefetchPaths

pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 23-Apr-2012 15:04:39   

Walaa wrote:

Context won't link entities or asociate them together, the Context is used to make sure you don't get more than one instance of the same entity.

If you want a connected graph, you'd better use PrefetchPaths

I need graph, so I am using prefetch paths, they work very well, but I have a performance problem with the Context. I use Context to make sure that all entities in my graph are unique. With context it takes 00:02:40.3620346 to fetch whole graph. Without context it takes 00:01:06.5076501 to fetch whole graph.

What happens if I fetch the graph without context and then add graph to context (root entity). Will context remove duplicates?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Apr-2012 19:00:35   

Using Context won't prevent duplicate entities from being fetched from the database, and hence there are no performance gain what so ever.

The entities are fetched eitherway, but the context discards the duplicate ones and use references of the already existing entities in the graph.

Fetching a huge graph would take longer time while using a Context, because it takes time to check the graph for a duplicate, before adding each entity to the graph.

pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 23-Apr-2012 19:38:19   

ok I understand your point, but I see difference in my code - most likely performance gain is due to the context performance never the less please answer my last question: if I fetch graph without context and then add root entity to context, will context work, or the context is honored only during fetch?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Apr-2012 20:08:17   

Entity objects can be added to the Context at any time, as well as Entity collection objects. When an entity object is added to a Context, its internally referenced related entity objects and entity collection objects are added to the same Context object as well. When an entity collection is added to a Context object, all its entity objects will be added to the Context and every entity object added to the entity collection after that point will be added to the same Context object the collection is added to as well, which makes it easy to work with a Context object, as it is mostly transparent.

Having said that, the context will not remove entities from the fetched collection. But those duplicate entities won't be added to the context object. (No reference to them would be added to the context).

And for that you will always get the same unique instance of any entity when you try to Get() it from the Context.

pokrec
User
Posts: 20
Joined: 05-Jan-2011
# Posted on: 23-Apr-2012 20:24:02   

A has 1:n relation to B. B has 1:n to C. I fetched collection of A objects, I used prefetch path so that at one call I can access A->B->C Then I add collection of A to context. Then I fetch collection of B and add it afterwards to context. Then I fetch collection of C and add it afterwards to context. Based on my test context replaced entities so that all A/B/C are unique. Based on your feedbak the A->B->C graph should be not chnaged. Can you comment on this?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Apr-2012 06:40:24   

If you are adding a collection to a Context, you should first add the collection and then perform the fetch. The same applies to PrefetchPaths. The best is to fetch with prefetchPath using the overload that contains.

Remember that the context provides a unique semantic context for entities. If an entity instance is already loaded in the used Context object, the entity data read from the database is not added to a new entity class instance, but the entity class instance already known by the context containing the same entity instance is updated. That, I think, is what Walaa meant to say.

All these is explained in detail in the documentation.

David Elizondo | LLBLGen Support Team