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
503
votes
10 answers

Entity Framework - Include Multiple Levels of Properties

The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the method below will return ApplicationServers with the included properties shown here. However, ApplicationsWithOverrideGroup is…
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
81
votes
5 answers

Laravel Eloquent: eager loading of multiple nested relationships

What laravel says: $books = App\Book::with('author.contacts')->get(); What I need is something like this $books = App\Book::with('author[contacts,publishers]')->get(); where we eager load multiple relationships within a relationship. Is this…
DrivingInsanee
  • 1,631
  • 2
  • 13
  • 22
81
votes
7 answers

Eloquent eager load Order by

I have problem with eloquent query. I am using eager loading (one to one Relationship) to get 'student' With the 'exam', Using the code below. Student::with('exam')->orderBy('exam.result', 'DESC')->get() And i want to order received rows by the…
Andrius
  • 931
  • 1
  • 6
  • 7
76
votes
7 answers

How to Determine if Rails Association is Eager Loaded?

Does anyone know a way to determine if a Rails association has been eager loaded? My situation: I have a result set where sometimes one of the associations is eager loaded, and sometimes it isn't. If it isn't eager-loaded, then I want to look up…
wbharding
  • 4,213
  • 2
  • 30
  • 25
74
votes
1 answer

Fetch vs FetchMany in NHibernate Linq provider

NHibernate eager loading can be done using Fetch and FetchMany, as described in NHibernate Linq Eager Fetching on Mike Hadlow's blog. What is the difference between these two methods and under what circumstance would each be used?
Simon
  • 1,499
  • 3
  • 17
  • 23
65
votes
3 answers

How do I eagerly Include the child and grandchild elements of an entity in Entity Framework Code First?

Imagine three entities (Customer, Book, Author) related like this: A Customer has many Books A Book has one Author I use that data to print a report like this: Customer: Peter Book: To Kill a Mockingbird - Author: Harper Lee Book: A Tale of Two…
adolfojp
  • 2,961
  • 4
  • 24
  • 21
43
votes
2 answers

EF Core returns null relations until direct access

I have some models like those below: public class Mutant { public long Id { get; set; } ... // Relations public long OriginalCodeId { get; set; } public virtual OriginalCode OriginalCode { get; set; } public int…
ConductedClever
  • 4,175
  • 2
  • 35
  • 69
38
votes
5 answers

Laravel - Eager Loading Polymorphic Relation's Related Models

I can eager load polymorphic relations/models without any n+1 issues. However, if I try to access a model related to the polymorphic model, the n+1 problem appears and I can't seem to find a fix. Here is the exact setup to see it locally: 1) DB…
Wonka
  • 8,244
  • 21
  • 73
  • 121
33
votes
4 answers

Entity Framework Core 2.0.1 Eager Loading on all nested related entities

I have a simple problem, but cant seem to find a way around it. I am using Entity Framework Core version 2.0.1 and want to eager load all my entities by default. Example: public class Order { public int Id { get; set; } public string Name {…
Jinish
  • 1,983
  • 1
  • 17
  • 22
32
votes
4 answers

Fighting cartesian product (x-join) when using NHibernate 3.0.0

I'm bad at math but I kind get idea what cartesian product is. Here is my situation (simplified): public class Project{ public IList Partners{get;set;} } public class Partner{ public IList Costs{get;set;} public…
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
31
votes
3 answers

Why can't Laravel/Eloquent use JOIN for Eager Loading?

belongsTo('User'); } } class User extends Eloquent { public function cats() { return $this->hasMany('Cat'); } } Now: $cats =…
empz
  • 11,509
  • 16
  • 65
  • 106
29
votes
1 answer

EF CTP5 - Strongly-Typed Eager Loading - How to Include Nested Navigational Properties?

Attempting to cutover our EF4 solution to EF CTP5, and ran into a problem. Here's the relevant portion of the model: The pertinent relationship: - A single County has many Cities - A single City has a single State Now, i want to perform the…
RPM1984
  • 72,246
  • 58
  • 225
  • 350
26
votes
8 answers

Force eager loading of otherwise lazy loaded properties

I've got a Hibernate object which's properties are all loaded lazy. Most of these properties are other Hibernate objects or PersistentSets. Now I want to force Hibernate to eager load these properties for just one time. Of course I could "touch"…
user321068
25
votes
3 answers

Eager loading child collection with NHibernate

I want to load root entities and eager load all it's child collection and aggregate members. Have been trying to use the SetFetchMode in FluentNHibernate, but I am getting duplicates in one of the child collection since I have a depth of 3 levels.…
Kristoffer
  • 1,633
  • 3
  • 19
  • 25
25
votes
4 answers

Eager Loading Using Fluent NHibernate/Nhibernate & Automapping

I have a requirement to load a complex object called Node...well its not that complex...it looks like follows:- A Node has a reference to EntityType which has a one to many with Property which in turn has a one to many with PorpertyListValue public…
nabeelfarid
  • 4,156
  • 5
  • 42
  • 60
1
2 3
99 100