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

PHP: Laravel how to eager load find method

I have a model Users which has-many Pages, I want to eager load the method below so that it returns only a single user with all the pages eager loaded, how do I go about it. $user = User::find(1); $pages = $user->pages(); foreach($pages as $page) { …
George
  • 3,757
  • 9
  • 51
  • 86
9
votes
1 answer

Linq to Entities - eager loading using Include()

I've got this really basic table structure: dbo.tblCategory dbo.tblQuestion (many to one relationship to tblCategory) dbo.tblAnswer (many to one relationship to tblQuestion) So basically, what I'm trying to do is when I load a category, I want to…
Jim B
  • 8,344
  • 10
  • 49
  • 77
9
votes
1 answer

Rails 3 eager loading of deep nested association

I am building a public activity stream which contains a stream of the following: User posted 3 minutes ago User starred a post I am using the public_activity gem for achieving this. My question is whether there is a way to use the includes for a…
swaroopsm
  • 1,389
  • 4
  • 18
  • 34
9
votes
1 answer

Eager Loading from the model in Laravel 4

In Laravel 3, one could do the following in the model (http://laravel.com/docs/database/eloquent#eager): class Book extends Eloquent { public $includes = array('author'); // this line public function author() { return…
Ben
  • 15,938
  • 19
  • 92
  • 138
8
votes
1 answer

Eager loading in EntityFramework with DbContext.Database.SqlQuery

In EF 4, can I do eager loading of navigation properties by writing sql on DbContext.Database.SqlQuery or DbContext.Set().SqlQuery? I don't seem to be getting my navigation properties populated. Edit It seems I can do eagerloading with…
enamrik
  • 2,292
  • 2
  • 27
  • 42
8
votes
5 answers

is there a way to eager load polymorphic association's associations?

artists have many activities (basically a cache of interactions between users): class Activity < ActiveRecord::Base belongs_to :receiver, :class_name => 'Artist', :foreign_key => :receiver_id #owns the stuff done "TO" him belongs_to :link,…
Homan
  • 25,618
  • 22
  • 70
  • 107
8
votes
0 answers

How to disable Lazy loading in EF Core 3.1.3?

I have tried to disable Lazy loading those two ways: 1) public SqlDbContext(DbContextOptions options) : base(options) { this.ChangeTracker.LazyLoadingEnabled = false; } 2) public void ConfigureServices(IServiceCollection…
8
votes
3 answers

Select specific fields in Eloquent eager loading not working

I have a Notifications model that belongs to a User. I want to load the user when a collection of notifications is selected and only load the model with names and emails. However, when using the select method, the query returns…
Birendra Gurung
  • 2,168
  • 2
  • 16
  • 29
8
votes
1 answer

Multiple level eager loading in Laravel?

Let's say I have these relationships class Invitation { public function invitedPeople() { return $this->belongsTo('App\Person', 'personID'); } } class Person { public function apartments() { return…
Luis Deras
  • 1,239
  • 2
  • 19
  • 48
8
votes
2 answers

Eager-loading using LINQ to SQL with Include()

I have spent 2 days bashing my head against this problem, and I can't seem to crack it (the problem that is). The same code was working fine until I added database relationships, and I have since read a lot about lazy-loading. I have two database…
Ian
  • 1,475
  • 2
  • 15
  • 31
8
votes
4 answers

Select specific columns from Eloquent relations

I have the following models in my application: User.php
user3718908x100
  • 7,939
  • 15
  • 64
  • 123
8
votes
1 answer

SQLAlchemy : eager loading relationships of relationships of relationship

I have this structure: class User(DeclarativeBase): ... teamMemberships = orm.relationship("TeamXREF",backref="user",lazy = "dynamic") class TeamXREF(DeclarativeBase): ... class Team(DeclarativeBase): ... name=db.Column(String) …
ptou
  • 323
  • 4
  • 12
8
votes
1 answer

In Rails, how can I eager load all code before a specific Rspec test?

I have some Rspec tests that are sometimes failing with an error: Circular dependency detected while autoloading constant. These tests are multithreaded (rufus-scheduler tests), which is apparently a known problem for autoloading code…
8
votes
1 answer

Unloaded "eager-loaded" properties causing issues when returning json'd data

Hopefully the title makes sense, I'll do my best to describe my problem. I'm currently developing an ASP.NET Web API. In my "Company" controller I have a GetCompany() action. /// /// Gets the 'Authorization-Token' request header…
Ben Black
  • 3,751
  • 2
  • 25
  • 43
8
votes
1 answer

Rails Eager Loading and where clause

I'm eager loading a model object with its associations: user= User.includes(:posts).find(1) But then at certain points in the code I would like to do something like this: user.posts.where(:topic => "x") But that just re-runs the query again. So…
user1069624
  • 561
  • 2
  • 9
  • 24