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

Accessing multiple relationships' data within one query

Logic: A company has many users, the users can create alerts, thus, many alerts belong to one user. Each alert also has a location relation, where one alert belongs to one location. Because this is quite a vast query, I have achieved this (I think…
user860511
0
votes
3 answers

Laravel 4 Eager Loading n+1 issue

I have troubleshoot all day with eager loading/n+1 issue, researched and read and watched tutorials about this issue, but haven't solved it yet. I have set up the relationships for the models, but when I passing in the data with a helper function I…
Vartox
  • 189
  • 3
  • 12
0
votes
1 answer

Sequelize Many-to-Many eager loading on pre-existing DB

I am attempting to setup my existing Express app with Sequelize. In the application, upon a user logging in, I perform a query to try to obtain an object that represents the user and several of the relations from a handful of tables. I have drawn…
Bobby
  • 1,439
  • 2
  • 16
  • 30
0
votes
2 answers

Is it possible to eager load arbitrary queries in Eloquent?

I'm working in Laravel 4, and I have a Child model with multiple EducationProfiles: class Child extends EloquentVersioned { public function educationProfiles() { return $this->hasMany('EducationProfile'); } } If I wanted to get all…
dspitzle
  • 740
  • 2
  • 9
  • 26
0
votes
1 answer

Laravel4 Eager Loading not possible

I've got following query which beats the crap out of my database, because it makes a a single query for every relation: $projects = $this->project->all()->sortBy(function ($item) { return $item->votes()->count(); }, SORT_REGULAR,…
Bernd Strehl
  • 2,852
  • 4
  • 24
  • 43
0
votes
1 answer

Rails 4: Odd Activerecord sort order

Like in this question I am looking to eager load another object and then sort the list based on a value from the eager loaded object. Given parent Foo and child Bar: class Foo < ActiveRecord::Base has_many :bar, foreign_key: :foo_name,…
0
votes
2 answers

What is the proper way to eager load associated objects for a 'has_many' association?

I am using Ruby on Rails 4 and I would like to eager load associated objects for a has_many association. That is, I have the following models: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base …
user502052
  • 14,803
  • 30
  • 109
  • 188
0
votes
2 answers

Laravel 4 eager loaded nested relation returned as array, I want Model

I am having problems accessing an object retrieved from a nested relationship. I can only access the nested relation as an array (it is returned as json). The other relation is returned fine as a Model. (I'm totally new to Laravel 4 and a lot of its…
hedboc
  • 1
  • 1
0
votes
1 answer

Laravel 4 eager loading - All relationships get shoved to first returned object

Ok, so I'm trying to figure out eager loading to improve application performance. I'm working on a service database, but am having some problems. Here is my code $service_data =…
Sidney
  • 624
  • 7
  • 20
0
votes
1 answer

How to eager load in WCF Ria Services/Linq2SQLDomainModel

I have a databound grid at my view (XAML) and the Itemsource points to a ReportsCollection. The Reports entity has three primitives and some complex types. These three are shown as expected at datagrid. Additionally the Reports entity has a property…
0
votes
2 answers

hibernate duplicate domains while getting data

I have two tables where there is a one to many relationship between those tables. here is a tables: Category table (Parent table) @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(name="CATEGORY_NAME") private String…
786543214
  • 855
  • 4
  • 14
  • 29
0
votes
1 answer

How to find all questions without any answers with Symfony 2?

I have two entities Question and Answer with appropriate relationships. I want an optimized way (less resource) to find all questions (only id and title) without answers with doctrine. Thanks for your answer.
foozoor
  • 463
  • 1
  • 5
  • 16
0
votes
1 answer

How to includes associated models in active_admin

In a Rails application, I have two models like this : class Painting < ActiveRecord::Base belongs_to :artist end class Artist < ActiveRecord::Base belongs_to :country def display_name text = to_s if birth_year death =…
Dougui
  • 7,142
  • 7
  • 52
  • 87
0
votes
2 answers

Using load('relation') with caching

I know you can cache a query by appending ->remember($minutes). However, this doesn't work when using loading on an existing Eloquent result. This is because load doesn't return a querybuilder but directly executes it. Say you want to load the…
edi9999
  • 19,701
  • 13
  • 88
  • 127
0
votes
1 answer

How to eager load deeper twice

in my Post model i try to eager load user (the owner of post), favorite, and comments with authors of each comment : Post.all.includes(:user, :original => {:favorite, :comments => :author }) but it doesn't work , i get error syntax error,…
medBouzid
  • 7,484
  • 10
  • 56
  • 86