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

why use lazy/eager loading database level

I am currently reading about eager vs lazy loading. I am confused and comparing it to load vs no-load. let's say i have two sections of UI.In one of them i want to show child data along with parent data and in second UI i am just showing parent…
Ankit Gupta
  • 2,529
  • 2
  • 15
  • 26
0
votes
1 answer

Yii Framework 2.0 Active Record

I am working with Yii framework 2.0, I have one database table that looks like the following. id active key 1 0 xx 2 1 xx 3 0 zzz 4 0 wwww 5 1 wwww 6 1 qqqqq I would like to get…
O Connor
  • 4,236
  • 15
  • 50
  • 91
0
votes
1 answer

Yii framework 2.0 pass parameter to eager loading relational database table

Working with Yii framework 2.0 I tried to retrieve data from my relational database tables following the documentation here http://www.yiiframework.com/doc-2.0/guide-db-active-record.html Below is the code sample under the section Lazy and Eager…
O Connor
  • 4,236
  • 15
  • 50
  • 91
0
votes
1 answer

Rails 4 eager loading a model within model

I have the following: class ObjectA < ActiveRecord::Base has_one :object_b end class ObjectB < ActiveRecord::Base belongs_to :object_a has_one :object_c end class ObjectC < ActiveRecord::Base belongs_to :object_b end So doing…
Yoni
  • 624
  • 5
  • 15
0
votes
1 answer

ActiveRecord find with include against table with records that might not be there

Here's my situation. I have a set of lists of words. Some words can appear in more than one list. For each word/list combination, I might have an example sentence. Words class Word < ActiveRecord::Base has_and_belongs_to_many :lists has_many…
Chris
  • 1,013
  • 1
  • 15
  • 35
0
votes
1 answer

Force Eagerly load all dependencies in Hibernate

Most of our Entities have Lazy loading as they are not required. In one specific scenario,function requires them to load eagerly, which is out of Transaction Scope. Here's the bit of implementation @Transaction public E…
RaceBase
  • 18,428
  • 47
  • 141
  • 202
0
votes
1 answer

How do I eager load two levels of associations in 1 call?

I have FamilyTree, Node, Comment, & User models. The relationship is like this: FamilyTree class FamilyTree < ActiveRecord::Base belongs_to :user has_many :memberships, dependent: :destroy has_many :members, through: :memberships, source:…
marcamillion
  • 32,933
  • 55
  • 189
  • 380
0
votes
1 answer

How to efficiently update associated collection in rails (eager loading)

I have a simple association like class Slot < ActiveRecord::Base has_many :media_items, dependent: :destroy end class MediaItem < ActiveRecord::Base belongs_to :slot end The MediaItems are ordered per Slot and have a field called ordering. And…
0
votes
1 answer

Querying while eager loading in rails

I have a News Model which has polymorphic association with ContentRoles I want to query news on created_at such that it fetches all the news greater than given timestamp my query currently looks something like this @news =…
level0
  • 323
  • 1
  • 3
  • 13
0
votes
1 answer

Eager loading when finding entities using a key vs a query

Does finding an entity using a key (like follows) eagerly load all related entities implicitly? var person = context.People.Find(1); Say the People entities have related Children entities, would they be eagerly loaded in the above lookup or would…
psand2286
  • 55
  • 1
  • 5
0
votes
2 answers

Laravel Eloquent ORM eager loading. Relation incorrectly returned as null

I have an Eloquent ORM relationship defined as follows: ProductConfiguration: public function product() { return $this->belongsTo('Excel\Products\Product'); } public function currency() { return…
Kevin Bradshaw
  • 6,327
  • 13
  • 55
  • 78
0
votes
1 answer

Laravel eagerloading not working

I am trying to utilize Laravels eagerlaoding functionality, but it doesn't at all behave like expected. I've tried following the official examples, but I cannot reproduce their behavior. I have a Product class which hasMany('OrderLine') meaning…
Niels B.
  • 5,912
  • 3
  • 24
  • 44
0
votes
1 answer

Unexpected Results in Laravel Eager Loading With Constraints

I have two tables: assets and asset_classifications. The tables have one-to-many relationship, where an asset has one asset classification, and asset_classifications have many assets. I'm trying to run a query to get all assets which has an…
christianleroy
  • 1,084
  • 5
  • 25
  • 39
0
votes
1 answer

Is there any annotation available for eager loading at class level when we are not using any mapping?

Is there any annotation available for eager loading at class level when we are not using any mapping ? I know that this is available in XML configuration where we have (I know that…
0
votes
1 answer

Spring MVC: findById - do not include all ManyToOne collections

I have 3 entities. ClassA, ClassB and Class C. ClassA have ManyToOne with ClassB and Class C. If I go from web browser to /classA/1 or /classA/abc/1 I got very big JSON with ClassA and all items from ClassB and ClassC. But I want only ClassA and …
martin
  • 1,707
  • 6
  • 34
  • 62