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

Rails Eager Load and Limit

I think I need something akin to a rails eager loaded query with a limit on it but I am having trouble finding a solution for that. For the sake of simplicity, let us say that there will never be more than 30 Persons in the system (so Person.all…
Chad M
  • 943
  • 1
  • 9
  • 22
6
votes
1 answer

Eager Fetching with nhibernate Criteria API

I am trying to use the criteria Api over multiple tables with eager loading. My stripped-down Entities look like this: class Limit { Risk {get; set;} } class Risk { List Companies { get;set;} } class Company { List
user631833
  • 61
  • 1
  • 2
6
votes
3 answers

How can I convert IQueryable to ObjectQuery?

I'm trying to apply the advice in this post: Tip 22 - How to make Include really Include It suggests a workaround for ensure eager loading works in the Entity Framework (4.2). That workaround involves casting the IQueryable to an…
dommer
  • 19,610
  • 14
  • 75
  • 137
6
votes
2 answers

LINQ to SQL eager loading with conditions

I'm trying to learn LINQ to SQL and i've found out about the LoadWith function. All the examples i've found will load all records from the table you specify in the LoadWith function e.g. var dlo = new DataLoadOptions(); dlo.LoadWith(b =>…
Alex
  • 34,776
  • 10
  • 53
  • 68
6
votes
2 answers

Serialize Entity Framework object with children to XML file

I'm querying data with parent/child result sets using Entity Framework and I want to export this data to an XML document. var agreement = storeops.Agreements.SingleOrDefault(a => a.AgreementNumber == AgreementTextBox.Text); XmlSerializer serializer…
6
votes
3 answers

JPA & Hibernate: Eager loading performing subsequent queries to fetch all data, instead of doing it in just one query

I have the following doubt. I would like to know why when using JPA and Hibernate, when performing an Eager loading in a ManyToOne or OneToMany relationship, it calls the DB in order to obtain the Entity information but additionally, produces…
6
votes
1 answer

EF4 and undesired loading of related collection with AddObject

I have an odd case where adding a record is causing unwanted loading of a related collection. For example, I have Requests and Sessions. A Session can contain many Requests. I already have loaded the session, and just want to add a new…
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
6
votes
1 answer

Laravel Dynamic Eager Loading for Dynamic Relationships

Laravel Version: 5.5 PHP Version: 7+ Database Driver & Version: mysql 5.7+ Scenario: I have a SaaS application that has flexible database structure, so its fields are bound to change, especially given it has a Json field (for any extra database…
6
votes
1 answer

How do I force active record (Ruby) eager loading at the model level?

We would like to force our Post model to eager load all comments. Right now we have to specify the eager loading at the find(:all), like below: Post.all(:include => [ :comment ]) Is there a way to force an eager loading default at the Post model…
Kevin Baker
  • 1,187
  • 3
  • 12
  • 22
6
votes
2 answers

Eager-load for custom joins in ActiveRecord

I have a table of restaurants that have many reservations. Now I would like to query all restaurants and in case they have reservations for 2 people it should preload these associations. In case there are no reservations for 2 it should still return…
Robert Strobl
  • 295
  • 2
  • 12
6
votes
3 answers

EF - how to prevent eager loading to load all nested entities

I've manay-to-many relationship between two entities: Categories <--> Items public class CategoryMaster { [Key] public int Id { get; set; } public string Name { get; set; } public virtual List SubCategories { get;…
RollerCosta
  • 5,020
  • 9
  • 53
  • 71
6
votes
1 answer

"TypeError: no implicit conversion of nil into String" when eager loading results

I'm using ruby '2.3.0' and 'rails', '3.2.22.2'. I need a little help & explanations about a query I've made. Here's my models: class AssessmentRaw < ActiveRecord::Base belongs_to :session has_many :schedulers, :class_name => 'MailingScheduler',…
fro_oo
  • 1,610
  • 4
  • 24
  • 46
6
votes
3 answers

Querying Relationship Existence using multiple MySQL database connections in Laravel 5.2

I am dealing with the following situation: I have two models, an Employee with id and name fields and a Telephone with id, employee_id and flag fields. There is also an one-to-many relationship between these two models, that is an employee may have…
ira
  • 5,569
  • 6
  • 30
  • 41
6
votes
1 answer

Fetch Type LAZY still causes Eager loading Hibernate Spring data

I have created a simple Spring boot project with Spring data. I have a TagGroup Entity which has one to many relation with Tags. @Entity @Table(name = "TAG_GROUP") public class TagGroup{ @OneToMany(fetch=FetchType.LAZY,mappedBy = "tagGroup") …
6
votes
1 answer

SQLAlchemy only loads collection, not backref when eagerloading

For example (eagerload/joinedload do the same thing): session = Session() parents = session.query(Parent).options(joinedload(Parent.children)).all() session.close() print parents[0].children # This works print parents[0].children[0].parent #…
Draemon
  • 33,955
  • 16
  • 77
  • 104