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

Eager loading of activerecord_reputation-system votes

Using Miniprofiler, I see that my blog index page is calling an SQL query for every blog post in order to find the number of votes. How can I eager load the votes so that I can consolidate these queries? The SQL I see is something like the following…
umezo
  • 1,519
  • 1
  • 19
  • 33
0
votes
2 answers

Filtering results from eager loading in Rails

I have a Box, which has many Compartments which contain many Items which belong to a Category. Items are polymorphic (:itemable) as they can belong to Boxes without Compartments - this might be my difficulty. I can call all Items back for all Boxes…
ritchielee
  • 268
  • 1
  • 9
0
votes
1 answer

Eager loading not loading in rails

Hi am I missing something here? I read the docs on including joins on load and tried the following below. You can see that when I use "include", the included join does not output the desired data, however when I explicitly call the join model after…
jdkealy
  • 4,807
  • 6
  • 34
  • 56
0
votes
1 answer

How to save the values in an Eager Loaded class?

To add a role into my user and save it: var user = DataContext.Users .Include("Roles") .Where(u => u.UserName.ToUpper() == username.ToUpper()) .FirstOrDefault(); foreach (string rolename in roleNames) { var role =…
Blaise
  • 21,314
  • 28
  • 108
  • 169
0
votes
1 answer

POCO entities and IsLoaded() in EF 4.1

I'm working on an enterprise application that leverages the repository pattern on top of EF 4.1 with eager loading to POCO entities. Typically, a call will look like this: public IEnumerable List(DateTime date) { using (var context =…
Thorsten Westheider
  • 10,572
  • 14
  • 55
  • 97
0
votes
1 answer

Eager loading failing for Child Collection in EF 4.3.1

I have the following classes: public class Configuration { public long Id {get;set;} public string Name {get;set;} public Expression Criteria {get;set;} } public class Expression { public long Id {get;set;} public string Value…
0
votes
1 answer

Eager Loading N+1 issue

<%- @lessons.includes(:bookmarks).each_with_index do |lesson, index| -%> <%- bookmark = lesson.bookmarks.where(user_id: current_user).first -%> <%= render :partial =>…
John
  • 4,362
  • 5
  • 32
  • 50
0
votes
1 answer

Doctrine loads all records twice

I have an entity called Category with the following fields: id: integer name: string slug: string children: OneToMany(targetEntity="Category", mappedBy="parent") parent: ManyToOne(targetEntity="Category", inversedBy="children") As you can see, each…
tamir
  • 3,207
  • 2
  • 33
  • 51
0
votes
1 answer

NHibernate load child elements/collections with Criteria API

How to I load child elements and child collections using the Criteria API. I have read just about every link I could find on google, but the child elements will not get loaded. Here is my setup Orders have OrderItems and that is specified in my…
getit
  • 623
  • 2
  • 8
  • 30
0
votes
1 answer

Eager loading nested, repeated models?

Lets say I have a project model which has many members and many tasks. class Project < ActiveRecord::Base has_many :memberships has_many :members, :through => :memberships, :class_name => 'User' has_many :tasks end When a task is created in…
David Tuite
  • 22,258
  • 25
  • 106
  • 176
0
votes
3 answers

NHibernate: Solving N+1 in master-detail where detail has many-to-one relationship?

I have an Order which has a collection of OrderLines. Every OrderLine has a many-to-one to a Product. For all Orders I have a searchwindow which - by default - shows all Orders in a grid. Using this same window I can filter all Orders which contain…
TedOnTheNet
  • 1,082
  • 1
  • 8
  • 23
0
votes
1 answer

EF Code First 4.3 conditionally load navigation property

Is it possible using DbContext / DbSet / DbQuery to conditionally eagerly load a navigation property. The query below will return parties that have a particular role. What I need is to additionally only load the matching roles. first attempt var…
Jim
  • 14,952
  • 15
  • 80
  • 167
0
votes
1 answer

How can I tell if eager loading is working?

So I've got a forum thread full of posts, and I want to eager load the users from the posts and those users roles, to reduce the total number of database queries (which seems to be the best way to do things) (to make things extra fun, forum and…
DVG
  • 17,392
  • 7
  • 61
  • 88
-1
votes
2 answers

How can I make these Active Record queries faster?

I'm using Active Record to send a few queries to a postgres database. I have a User model, a Business model and a joiner named UserBiz. The queries I mentioned go through the entire UserBiz collection and then filter out the businesses that match…
-1
votes
3 answers

laravel dynamic relationship property performance and alternative way

I have a question about the dynamic relationship properties, suppose there are user and address model. public function address(){ return $this->hasMany(Address::class); } I want to ask $user->address will cause N+1 problems , therefore, we may…
yuk
  • 49
  • 8
1 2 3
99
100