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
4
votes
1 answer

Laravel Eloquent eager loading: children are missing parent reference (sort of N+1)

I am currently running on Laravel 5.1.19 and am observing the following issue: Assume the following models (Students and Teachers as example): class Teacher extends \Illuminate\Database\Eloquent\Model { public function rel_students() { …
marstato
  • 363
  • 2
  • 12
4
votes
1 answer

How to perform eager loading on model descendants using Rails and Ancestry

As for now I'm working on a blog application that has article/comment models connected via has_many/belongs_to associations. To create nested comments functionality I use ancestry gem. However, I would like to eager load all of the descendants of a…
kushnir
  • 53
  • 5
4
votes
1 answer

Laravel Nested Eager Loading with Constraints

I have a Question Eloquent Model, a Course Eloquent Model, a University Eloquent Model. A One to Many relationship exists between the University and the Course. A Many to Many relationship exists between the Question and the Course. The Three models…
Nikhil Agarwal
  • 410
  • 4
  • 15
4
votes
1 answer

Hibernate eagerly loads lazy reference

I have three Entities A, B and C. @Entity public class A { @Id private long id; @OneToOne(fetch = FetchType.EAGER) @JoinColumn(name = "ID", insertable = false, updatable = false) public B b; } @Entity public class B { @Id private…
Thomas
  • 624
  • 11
  • 28
4
votes
2 answers

Entity framework many-to-many relation table created "backwards"

I'm having an issue with EF6 and many-to-many relations. I have a following setup: public class Foo { public int Id { get; set; } public virtual ICollection Bars { get; set; } public virtual ICollection SubBars { get; set;…
Sami
  • 2,050
  • 1
  • 13
  • 25
4
votes
1 answer

Rails 4 Eager load limit subquery

Is there a way to avoid the n+1 problem when eager loading and also applying a limit to the subquery? I want to avoid lots of sql queries like this: Category.all.each do |category| category.posts.limit(10) end But I also want to only get 10 posts…
Andy Hadjigeorgiou
  • 603
  • 2
  • 10
  • 18
4
votes
3 answers

Is there a way to Include() all with dbcontext?

When querying a DbContext with eager loading, it is required to Include("Navigation") in order to populate Navigation Properties. However on some occasions I would like to simply Include all navigation properties for an entity. Is there a method…
Tevis
  • 729
  • 1
  • 12
  • 27
4
votes
0 answers

how to access eloquent eager loading collection in blade template?

I fetch posts from a database with answers to them and the post author (user). The result looks good in dd($posts). I can see all needed information. the model and relationships: class Post extends Model { public function postanswers() { …
haheute
  • 2,129
  • 3
  • 32
  • 49
4
votes
2 answers

nHibernate - eager fetching a list with child lists already populated

I have some objects: Public Class Person() { public int Id {get;set;} public IList Accounts {get;set;} public string Email {get; set;} } public class Account(){ public int Id {get;set;} public IList
Paul
  • 5,514
  • 2
  • 29
  • 38
4
votes
3 answers

Rails - eager load the number of associated records, but not the record themselves

I have a page that's taking ages to render out. Half of the time (3 seconds) is spent on a .find call which has a bunch of eager-loaded associations. All i actually need is the number of associated records in each case, to display in a table: i…
Max Williams
  • 32,435
  • 31
  • 130
  • 197
4
votes
2 answers

Eager Loading with where

I want to list all the garages by the car id, but the way I'm doing still returning informations about garages even if the car isn't found, the opposite happen, instead of nothing return, the garages returns with the car relations as empty. Let me…
yayuj
  • 2,194
  • 3
  • 17
  • 29
4
votes
1 answer

Entity Framework Eager Loading Loads Everything

We are using Entity Framework + Repository Pattern in a web based application to fetch database . Because of our complex business, our models are getting complex sometimes and this cause strange behaviour at Entity Framework eager loading…
4
votes
2 answers

How do we load related objects (Eager Loading) in Dot Net based Azure Mobile Service?

If I have following model structure public class QuestionItem: EntityData { public string Content { get; set; } public bool IsAnswered { get; set; } public int NumberOfAnswers { //todo: make it computable get; …
Supreet
  • 831
  • 1
  • 9
  • 30
4
votes
2 answers

Entity Framework, eager loading and big object graph

I have a WPF project that works on a local database with projects. The local databased is later synced with a server. Each project have multiple systems, and each of these have multiple "tags". The tag have a product, and the product consist of…
Trixz
  • 190
  • 13
4
votes
2 answers

Why won't these nested unscoped blocks work to remove default_scope in Rails 4 when they work in Rails 3?

My question It seems like Rails 4 is ignoring nested unscoped blocks (whereas they were ok in Rails 3). I've been Googling like crazy and can't find anything indicating a change here. Any ideas how I can get this working in Rails 4? What I'm…