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

Extend custom Rails preloader with includes

I'd like to trigger eager loading on a relation with an #includes call, that was modified with a custom preloader using ActiveRecord::Associations::Preloader on a Rails 7 app, Ruby 3.2: class Product < ApplicationRecord belongs_to :vendor …
23tux
  • 14,104
  • 15
  • 88
  • 187
4
votes
2 answers

Eager loading and group by in PostgreSQL

I know PostgreSQL, unlike MySQL, requires to list all selected fields in the group by clause when using aggregate functions, e.g. Transaction.select('sum(amount), category_id').group('category_id') Fair enough. But when I try to eager load an…
Pierre
  • 8,368
  • 4
  • 34
  • 50
4
votes
2 answers

Rails 3 Eager Loading with conditions - how to access eager loaded data?

I have a lot of cases in my app where a user has no more than one object (say, a "Description") within its association to another object (a "Group"). For example: class User < ActiveRecord::Base has_many :descriptions has_many :groups class…
kateray
  • 2,066
  • 4
  • 18
  • 23
4
votes
1 answer

Rails 6.1 eager load ActiveStorage::VariantRecord

I have been eager loading ActiveStorage attachments as follows: Journey.includes(created_by_user: [profile_picture_attachment: :blob]) We have been using variants and every since we upgraded to Rails 6.1 and enabled tracking Active Storage variants…
aBadAssCowboy
  • 2,440
  • 2
  • 23
  • 38
4
votes
1 answer

Rails: Structuring a query involving a polymorphic association and STI

I'm trying to find the 10 most recent comments on photos so I can integrate them into an activity feed on my Rails 3.0.3 application. I've got a Photo model, which inherits from an Upload model using single table inheritance: class Upload <…
4
votes
6 answers

How to avoid Eager column to be initialized in hibernate criteria for an Entity

Multiple Entities are associated with @OnetoMany relations and Eager Loaded with an Entity. While fetching data using Hibernate criteria, the related Eager column is initialized which is causing performance issue for my project as it is huge and…
4
votes
1 answer

Can I manually eager load a model with data in Laravel Eloquent?

For example say I have Book and Author models. I'd eager load the authors like this: $books = Book::with('author')->get(); But I have already fetched the list of authors with Author::all() in order to display them elsewhere on the page (a select…
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
4
votes
1 answer

How to eagerly load a many to many relationship with the entity framework code first?

I'll give the most basic example that I can think of for the purpose of clarity. Lets say that I have two entities of the following form: public class Student { public int Id {get;set} public string FullName {get;set;} public virtual…
4
votes
3 answers

Hibernate eager loading (fetch all properties does not work)

Basically I want to eager-load properties. I have the following HQL query: SELECT u.id AS id, u.name AS text, u AS obj FROM User AS u fetch all properties I would expect this to execute one query only. Instead I got N+1 queries. The code is the…
vbence
  • 20,084
  • 9
  • 69
  • 118
4
votes
1 answer

How can I chain ->makeHidden() when eager-loading a belongsToMany relationship?

Does anyone have any suggestions for how to use makeHidden() when eager-loading? here is my code: $work=Work::with([ 'work_category'=>function($query){ $query->with(['companies'=>function($query){ …
Claude Kirke
  • 80
  • 1
  • 8
4
votes
4 answers

Angular 6: Convert eager loading to lazy loading

I have a complete angular app that uses eager loading. I want to convert it to lazy loading, but because I have guard on all of my routes and all of them are sub routes to one main route that is guarded, I don't know if it's possible to do it and…
4
votes
1 answer

Include() in EF4 using RIA Services Domain Service not loading!

I am having trouble returning multiple entities (eager loading) using the Include() method. I am trying to load the entites in a silverlight application using RIA services. The Data Model consists of the following relationships: Events.ID =…
4
votes
2 answers

Good behaviour for eager loading multiple siblings and grandchildren (cousins?) in NHibernate 3.0 Linq

I'm trying to do the following with NHibernate 3.0's LINQ interface. I want to query for an object (using some Where clause), and load some children and grandchildren. Currently I'm doing it like so: var results = session.Query() …
sinelaw
  • 16,205
  • 3
  • 49
  • 80
4
votes
1 answer

Laravel eager load model from a hasMany relationship

There are 3 tables: users: id, name compliments: id, name users_compliments: id, compliment_id, user_id, complimentor_id Here is my User model: class User extends Model public function sent_compliments() { return …
Yaseen Aniss
  • 151
  • 1
  • 9
4
votes
0 answers

Eager loading - How can I update entity includes some related entities?

I have a parent table (from oracle dataBase) and some child tables, and I can successfully update the parent entity using the Update method below, but not the child ones. By the way, can I use with generic entities here? public void UpdateEnt(T…
user1012506
  • 2,048
  • 1
  • 26
  • 45