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
22
votes
3 answers

Disable eager relations

In my project I have many Eloquent models that have eager relations configured in class like this: protected $with = [ 'countries', 'roles' ]; But sometimes I need just old plain model without any relations. Can I somehow…
Yauheni Prakopchyk
  • 10,202
  • 4
  • 33
  • 37
22
votes
7 answers

Eager loading and repository pattern

I'm wondering how to properly handle eager-loading problem for complex object graphs when using Repository pattern. This isn't ORM specific problem i guess. First try: public interface IProductRepository : IRepository { Product…
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
21
votes
4 answers

How to eager load associations with the current_user?

I'm using Devise for authentication in my Rails app. I'd like to eager load some of a users associated models in some of my controllers. Something like this: class TeamsController < ApplicationController def show @team =…
David Tuite
  • 22,258
  • 25
  • 106
  • 176
21
votes
2 answers

Can SQLAlchemy eager/joined loads be suppressed once set up?

I've got a case where most of the time the relationships between objects was such that pre-configuring an eager (joined) load on the relationship made sense. However now I've got a situation where I really don't want the eager load to be…
Russ
  • 10,835
  • 12
  • 42
  • 57
21
votes
2 answers

How addSelect() Builder method works in Laravel

Say I have these models: User Model namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * The table associated with the model. * * @var string */ protected $table = 'User'; …
Luis Deras
  • 1,239
  • 2
  • 19
  • 48
20
votes
2 answers

Eager loading: The right way to do things

I am running Ruby on Rails 3.1. I read the following articles and documentations about eager loading and I would like to find a right way to do things: Eager Loading Associations [Official documentation] ActiveRecord::Associations::ClassMethods…
Backo
  • 18,291
  • 27
  • 103
  • 170
20
votes
2 answers

Loading Nested Entities / Collections with Entity Framework

I am trying to Eagerly load all the related entities or collection of Entity in one call. My Entities Looks like: Class Person { public virtual long Id { get; set; } public virtual string FirstName { get; set; } public virtual string…
19
votes
2 answers

NHibernate: Why does Linq First() force only one item in all child and grandchild collections with FetchMany()

Domain Model I've got a canonical Domain of a Customer with many Orders, with each Order having many OrderItems: Customer public class Customer { public Customer() { Orders = new HashSet(); } public virtual int Id {get;set;} …
rbellamy
  • 5,683
  • 6
  • 38
  • 48
19
votes
2 answers

EF: Lazy loading, eager loading, and "enumerating the enumerable"

I find I'm confused about lazy loading, etc. First, are these two statements equivalent: (1) Lazy loading: _flaggedDates = context.FlaggedDates.Include("scheduledSchools") .Include …
Cynthia
  • 2,100
  • 5
  • 34
  • 48
18
votes
4 answers

Entity Framework - what's the difference between using Include/eager loading and lazy loading?

I've been trying to familiarize myself with the Entity Framework. Most of it seems straight forward, but I'm a bit confused on the difference between eager loading with the Include method and default lazy loading. Both seem like they load related…
Major Productions
  • 5,914
  • 13
  • 70
  • 149
18
votes
2 answers

Rails Eager Loading on All Finds

OK, I've been playing around with some of the eager loading things, and have 2 models something like: Class Recipe < ActiveRecord::Base belongs_to :cookbook has_many :recipetags end and Class Cookbook < ActiveRecord::Base has_many…
Cameron Ferroni
  • 183
  • 1
  • 4
17
votes
2 answers

What's the performance impact of disabling eager_load in production.rb?

My rails 4.1 app connects to a second, non-primary server via SSH for a backend jobs. Consequently, when the rails app restarts daily, the SSH connection needs to be live/up (rather the second, non-primary server needs to live/up), otherwise the the…
user1322092
  • 4,020
  • 7
  • 35
  • 52
17
votes
2 answers

Eager load associations with Active Model Serializers

Background I have a rails application with deeply nested associations. .-< WorkPeriod Timecard -< Week -< Day -<--< Subtotal `-< Adjustment -< (has many) I'm using Active Model Serializer to…
16
votes
2 answers

Rails 3 Limiting Included Objects

For example I have a blog object, and that blog has many posts. I want to do eager loading of say the first blog object and include say the first 10 posts of it. Currently I would do @blogs = Blog.limit(4) and then in the view use…
16
votes
2 answers

How to eager load sibling data using LINQ to SQL?

The goal is to issue the fewest queries to SQL Server using LINQ to SQL without using anonymous types. The return type for the method will need to be IList. The relationships are as follows: Parent Child1 …
Scott
  • 183
  • 2
  • 6