Questions tagged [eager-loading]

Eager loading is a way to load objects of a certain class and a number of named associations in the same request.

When you load records from the database and also want to access the associated objects for each of these records, it’s a good idea to make use of eager loading. Eager loading reduces the amount of queries made to the database and therefore increases performance.

This is one of the easiest ways of to prevent the dreaded 1+N problem in which fetching 100 posts that each need to display their author triggers 101 database queries. Through the use of eager loading, the 101 queries can be reduced to 2.

# Rails 2
Blogpost.all(:include => :comments)

# Rails 3
Blogpost.includes(:comments).all

# Entity Framework
Blogpost.Include(bp => bp.Comments);
1515 questions
13
votes
1 answer

Entity Framework 4.1 default eager loading

I'm using Entity Framework 4.1 code first approach. I want to make eager loading as my the dafault configuration, and by that avoid using the Include extension method in each fetching query. I did as recomended in MSDN, changing the simple lazy…
Sean
  • 141
  • 1
  • 3
13
votes
1 answer

NHibernate: How to perform eager subselect fetching of many children & grandchildren (object graph) in a single round-trip to the database?

First, please don't try to argue me out of doing the eager load - traversing the object graph and causing (by lazy loading) even more than ONE round-trip to the database is just not an option. I have a big object graph. I want to fetch the root…
sinelaw
  • 16,205
  • 3
  • 49
  • 80
13
votes
2 answers

Laravel: Where selection for Eloquent Eager Loading relationship

I got two DB tables: Posts $table->increments('id'); $table->integer('country_id')->unsigned(); $table->foreign('country_id')->references('id')->on('countries'); Countries $table->increments('id'); $table->string('name', 70); I use laravel as…
HelloWorld0815
  • 610
  • 4
  • 11
  • 29
13
votes
4 answers

Entity Framework - Eager loading related entities

I'm using Entity Framework 4 with MVC and need to ensure any referenced entities I want to use in my view have been loaded before the controller method returns, otherwise the view spits out the dreaded: The ObjectContext instance has been disposed…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
13
votes
3 answers

Eager loading with parameters - laravel

I have Banks table and separate table with services $bank = Banks::find(1); echo $bank->service(1); // print bank with that service (serviceId 1) It is posible to eager load all banks with service_id =1 ..somewhat…
sumit
  • 15,003
  • 12
  • 69
  • 110
12
votes
3 answers

How select referenced entity in nhibernate queryover

I Have a entity with a property referencing other entity (ReferenceEntity in examples). With HQL i can do this: select e.ReferenceEntity from Entity e where e.Id = :entityId NHibernate will give me the ReferenceEntity instance without lazy. With…
12
votes
4 answers

Dagger 2 - how to create/provide a EagerSingleton

I am having trouble with the Dagger 2 dependency injection framework. I would like to create an EagerSingleton. I assume that dagger 2 creates lazy loaded singletons when I use the @Singleton annotation. How do I create EagerSingleton using the…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
12
votes
4 answers

Can I have SQLAlchemy do subquery eager loading without repeating the full original query?

Suppose we have original generated query like that: SELECT company.x AS company_x, ... FROM company LEFT OUTER JOIN acc ON acc.id = company.acc LEFT OUTER JOIN usercomp_links ON company.id = usercomp_links.pid LEFT OUTER JOIN usergro_links ON acc.id…
Mihail Krivushin
  • 480
  • 1
  • 5
  • 13
12
votes
1 answer

Using Include() with inherited entities

In EF eager loading related entities is easy. But I'm having difficulties including inherited entities when loading data using table-per-type model. This is my model: Entities: ArticleBase (base article entity) ArticleSpecial (inherited from…
Peter Stegnar
  • 12,615
  • 12
  • 62
  • 80
12
votes
2 answers

Laravel Eloquent nested relations returns data only on the first element

Intro Im havning some trubble getting the data on all the related elements. Im using Laravel as a REST backend service, exposing Json to the front-end javascript application. Data structure Consider I have the following tables: +----------------+…
R. Gulbrandsen
  • 3,648
  • 1
  • 22
  • 35
11
votes
2 answers

Eloquent Count nested relationships with nested eager loading

You must have seen the following feature (on facebook), a post with some comments, each comment has a like counter. https://img4.hostingpics.net/pics/67853820170616003640LaravelNewsAccueil.png In laravel it would be something like Post hasMany…
darkylmnx
  • 1,891
  • 4
  • 22
  • 36
11
votes
2 answers

Rails 4 eager load for an object

I'm trying to eager load an association from an instantiated object, i.e. instead of reading the associations together with parent object... User.includes(:characters).first ...defer it until I decide it's really needed and do something like: u =…
Kombajn zbożowy
  • 8,755
  • 3
  • 28
  • 60
11
votes
3 answers

Laravel 4.1 Eager Loading Nested Relationships with Constraints

I am trying to get Eager-Loading nested relationships, with constraints, to work. Everyone seems to be giving the same example of eager-loading nested relationships: $users = User::with('posts.comments')->get(); What I want to do instead is get all…
kJamesy
  • 5,973
  • 5
  • 20
  • 22
10
votes
2 answers

Spring data findAll() does not fetch eagerly

I have two entities with unidirectional one to many relationship. @Entity public class Basket { @Id @GeneratedValue private Long id; private int capacity; } @Entity public class Item { @Id @GeneratedValue private Long…
k13i
  • 4,011
  • 3
  • 35
  • 63
10
votes
1 answer

laravel eager loading using with() vs load() after creating the parent model

I am creating a Reply model and then trying to return the object with it's owner relation. Here is the code that returns an empty object: //file: Thread.php //this returns an empty object !!?? public function addReply($reply) { $new_reply =…
Yeasir Arafat Majumder
  • 1,222
  • 2
  • 15
  • 34