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
6
votes
2 answers

laravel 5.1 getting related 5 news of each category in many-to-many relation

I got stuck here been trying from 2-3 hours. I have a many to many relation: class Category extends Model { public function news() { return $this->belongsToMany('App\News'); } } class News extends Model { public function…
sanu
  • 1,048
  • 3
  • 14
  • 28
6
votes
2 answers

Laravel 4.1 eager loading

I'm having trouble on the eager loading. Let's say I have models of Members, TrainingCategory, TrainingCategoryResult and Registration Member Model: public function registration() { return $this->hasMany('Registration', 'member_id'); } public…
Theren
  • 383
  • 1
  • 3
  • 9
6
votes
1 answer

the logic of lazy loading with navigation properties in entity framework

I am having trouble understanding how lazy loading works. For example, in the following example, I can access the Courses of Students within Where() clause: context.Students .Where(st=>st.Courses …
renakre
  • 8,001
  • 5
  • 46
  • 99
6
votes
1 answer

Explicit loading of multiple references/collections on entity

Consider following entity model: public class Parent { public virtual FirstChild FirstChild { get; set; } public virtual SecondChild SecondChild { get; set; } } In my code, I have loaded Parent entity: Parent parent =
lxa
  • 3,234
  • 2
  • 30
  • 31
6
votes
1 answer

laravel - eloquent - get sum of related model specific column

assuming that I have the table orders with fields id, userId, amount, description and the table user with various fields how if I wand to get all the users (with all its fields) and also the sum of the "amount" column of the orders related…
ciccioassenza
  • 233
  • 1
  • 7
  • 17
6
votes
2 answers

Eager loading with has_many through

I have a User model. A user has many integrations. An integration is join to a profile via integration_profiles which contains a column data. I want to eager load all of a user's profiles. class Integration < ActiveRecord::Base has_many…
6
votes
1 answer

Rails Admin Eager loading relations for list view

We have a list view for a model Ticket in rails admin that loads very slowly. class Ticket < ActiveRecord::Base belongs_to :crew end The reason it is slow is that we display the ticket's crew relation through a method rails_admin_pretty_print…
zimkies
  • 1,067
  • 1
  • 9
  • 20
6
votes
1 answer

Rails add custom eager load

I have a number of custom find_by_sql queries in Rails. I would like to use eager loading with them but there doesn't seem to be a good way to do this. I have seen the eager_custom.rb file floating around and it doesn't seem to work with Rails now.…
riley
  • 2,387
  • 1
  • 25
  • 31
6
votes
3 answers

virtual keyword, Include extension method, lazy loading, eager loading - how does loading related objects actually work

Loading related object in MVC can be pretty confusing. There are lots of terms you need to be aware of and learn if you really want to know what you're doing when writing your entity model classes and controllers. A couple of questions that I've had…
PussInBoots
  • 11,028
  • 9
  • 52
  • 84
6
votes
1 answer

How to eager load a collection in ebean?

What is the correct way to eager fetch a nested collection in ebean and Play Framework 2? I tried this: Registration registration = find .fetch("participants") .fetch("participants.fieldValues") .fetch("participants.fieldValues.field") …
TomahawkPhant
  • 1,130
  • 4
  • 15
  • 33
6
votes
2 answers

Include not working with Entity Framework query

I'm not sure what changed, but, after coming back to an application I was working on a few weeks ago, my .Include() call is no longer working for for one of my related tables. The weird part is that it works for a different table. Here is some…
Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
5
votes
3 answers

Configuring EF to throw if accessing navigation property not eager loaded (and lazy-load is disabled)

We have a few apps that are currently using an EF model that has lazy-loading enabled. When I turn off the lazy-loading (to avoid implicit loads and most of our N+1 selects), I'd much rather have accessing a should-have-been-eager-loaded (or…
5
votes
1 answer

self-join eager loading recursively?

I have a self-joined model: class Comment < ActiveRecord::Base belongs_to :parent, :class_name => 'Comment', :foreign_key => 'parent_id' has_many :children, :class_name => 'Comment', :foreign_key => "parent_id" end Later I wish to first…
Razor Storm
  • 12,167
  • 20
  • 88
  • 148
5
votes
1 answer

ActiveRecord::ConnectionNotEstablished error (eager loading?)

I'm developing on OSX and deploying on Linux. My environments are: Development: OSX Lion Ruby 1.9.2p180 ActiveRecord 3.0.9 PostgreSQL 9.0 Test: Ubuntu Server 11.04 Ruby 1.9.2p290 ActiveRecord 3.1.1 PostgreSQL 9.1 The following pieces of code work…
Ecil
  • 1,009
  • 1
  • 13
  • 28
5
votes
1 answer

How to avoid N+1 queries in rails dependent destroy?

Class User < ActiveRecord::Base has_many :posts, dependent: :destroy end Class Post < ActiveRecord::Base belongs_to :user end When a User having N posts is destroyed, N+1 queries are run to destroy the associated posts and the user. How to…