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

Rails .joins doesn't load the association

Helo, My query: @county = County.joins(:state) .where("counties.slug = ? AND states.slug = ?", params[:county_slug]) .select('states.*, counties.*') .first! From the log, the SQL looks like this: SELECT states.*,…
0
votes
1 answer

GORM Eagerly Fetch Collection With Criteria

I have a simple one-to-many association: class Foo { int id Date someDate static hasMany = [ bars: Bar ] } class Bar { Foo foo Date someDate static mapping = { ..... columns { …
Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67
0
votes
2 answers

EF nested tables eager loading?

My table relations: Categories has many Posts has many PostImages I want to retrieve Categories>LastPost>FirstPostImage. And I tried something like following: var categories = entity.Categories .Where(x => x.PositionId == 2) …
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
0
votes
1 answer

Navigating through relations on two paths in schema

From top to bottom I have the belongs_to relationship between my tables , and well has_many from the other direction. ReportTarget Report Manager Organization and Score Manager Organization so notice that Report table and Score table are kind of…
user1899082
0
votes
1 answer

Navigating through relations for more than two level deep

From top to bottom I have the belongs_to relationship between my table , and well has_many from the other direction. ReportTarget Report Manager Organization I want to do an eager load on these. I think I can go this deep but I am not aware of the…
user1899082
0
votes
1 answer

LinqToSql: stop eager loading

Having set the following options to eager load a customer's orders: DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith(c => c.Orders); db.LoadOptions = dlo; How would I then stop this and revert back to lazy loading the orders…
ajbeaven
  • 9,265
  • 13
  • 76
  • 121
0
votes
1 answer

How do you eager load data via Alias in Fetch method?

Ok so I have the following relationships: Bulletin < BulletinEmailLog < BulletinEmailLogRecipient - Contact - Entity Where the "<" stands for 1 to many and the "-" is one to one. And here is the code: BulletinEmailLog BulletinEmailLogs = null; …
0
votes
1 answer

Making serializable_hash eager load

I have a function, Appointment#serializable_hash, that looks like this: def serializable_hash(options={}) options = { :methods => %w( notes client services products has_payments …
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
0
votes
1 answer

Opposite of includes in Rails

Assuming I have #post.rb class Post < ActiveRecord::Base default_scope includes(:user) what do I do when I don't want to include the user when I fetch a post? for instance, when I delete a post
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
0
votes
1 answer

How to Eager Load (Two-Level Upward)?

The models look like this: Customer (Model) - Order (Model) -- Product (Model) --- Color (Model) - Name (Property) When listing the details of Product, I want to show the Name of Customer (Name is a property of the model Customer). The code I'm…
Jim
  • 1,695
  • 2
  • 23
  • 42
0
votes
4 answers

NHibernate, fetch entity and eager load part of child collection

I've come unstuck on how to do this: I have a country entity. A country has zero or more facts. A fact has property called Year. How can I load a Country entity by its name and eagerly fetch all its facts from a given year, e.g. 2011 (but not load…
Øyvind
  • 1,600
  • 1
  • 14
  • 33
0
votes
1 answer

Eager loading works until special case

I have a Staff model, which has many staff_orders class Staff < Ressource has_many :staff_orders The staff_orders contain a from_date and a to_date. During this time the staff member is occupied. I want to create a dropdown list of staff…
Ralph Allen
  • 146
  • 1
  • 4
0
votes
1 answer

NHibernate Eager Load

I've got an application that has the following HQL: select distinct p from Position p inner join fetch p.RequiredSkills rs inner join fetch rs.Skill s where s.Id in (:skills) I've searched the positions where the position has the skill searched…
Jamez
  • 1,559
  • 1
  • 15
  • 26
0
votes
1 answer

Generic Include on ObjectQuery doesn't actually include anything

Today I tried to modify my existing generic GetAll method to work with eager includes. I have Lazy loading enabled, but I want to do eager loading for some specific caching optimizations. public IQueryable GetAll(List
Michiel Cornille
  • 2,067
  • 1
  • 19
  • 42
0
votes
1 answer

Eager loading via user defined function in Entity Framework 4.1

I’m using Entity framework 4.1. Fluent API to map my model to pre-defined database. Is there any way to use eager initialization of complex properties via stored procedures or user defined functions? For example: public class Car { public int Id…
1 2 3
99
100