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
16
votes
2 answers

org.hibernate.LazyInitializationException at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel

Despite of FetchType.EAGER and JOIN FETCH, I get a LazyInitalizationException while adding some objects to a @ManyToMany collection via a JSF UISelectMany component such as in my case the . The @Entity IdentUser, with…
codeSnippet
  • 181
  • 2
  • 7
15
votes
4 answers

How do I get Rails to eager load counts?

This is related to a question a year and change ago. I put up an example of the question that should work out of the box, provided you have sqlite3 available: https://github.com/cairo140/rails-eager-loading-counts-demo Installation instructions (for…
Steven
  • 17,796
  • 13
  • 66
  • 118
15
votes
4 answers

How do I wrap Linq2NHibernate's .Fetch and .ThenFetch inside my abstract repository?

I'm using a generic repository that exposes an IQueryable like this: public IQueryable AllEntities { get { return session.Query(); } } I can query like this: var results = (from e in repository.AllEntities …
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
15
votes
3 answers

eager loading not working with order() clause in rails

I am using this query to get my data user = User.includes(:skills).order(user: :id) it is working fine. but when i try to display skills by alphabetical order like below user.skills.order(name: :asc) It shows in logs that it goes in the database…
15
votes
1 answer

What does Rails.application.eager_load! do?

I came across the following line in a Rails application I'm maintaining: Rails.application.eager_load! I googled it but didn't find anything that really spelled out what it does. What does it do?
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
15
votes
3 answers

Using repository pattern to eager load entities using ThenIclude

My application uses Entity Framework 7 and the repository pattern. The GetById method on the repository supports eager loading of child entities: public virtual TEntity GetById(int id, params Expression>[] paths) { var…
15
votes
2 answers

Disable all lazy loading or force eager loading for a LINQ context

I have a document generator which contains queries for about 200 items at the moment but will likely be upwards of 500 when complete. I've recently noticed that some of the mappings denote lazy loading. This presents a problem for the document…
Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
14
votes
2 answers

Eager loading with route model binding

I have a controller function like this public function show(NovelRequest $request, Novel $novel) { // load the chapters $novel->chapters; // return the detail view of a novel return view('novels.show', compact('novel')); } I…
Frnak
  • 6,601
  • 5
  • 34
  • 67
14
votes
4 answers

What is the equivalent of @ManagedBean(eager=true) in CDI

As we all know that it is recommended to use annotations from javax.enterprise.context instead of javax.faces.bean as they are getting deprecated. And we all found ManagedBeans with eager="true" annotated with @ApplicationScoped from…
Jalal Sordo
  • 1,605
  • 3
  • 41
  • 68
14
votes
6 answers

Laravel eager loading with limit

I have two tables, say "users" and "users_actions", where "users_actions" has an hasMany relation with users: users id | name | surname | email... actions id | id_action | id_user | log | created_at Model Users.php class Users { public…
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
14
votes
1 answer

Django eager loading in many to many

Those are many models class FacultyMember(models.Model): # some attributes class Publication(models.Model): # some attributes author = models.ManyToManyField(FacultyMember, blank=True) class Project(models.Model): # some…
14
votes
4 answers

Fragment caching and eager loading: How to get the best of both worlds?

It seems to me that fragment caching and eager loading are -- at least sometimes -- somewhat at odds with each other. Let's say I have a User who has many posts which each has many comments which in turn also can have many comments and so on. When I…
KaptajnKold
  • 10,638
  • 10
  • 41
  • 56
13
votes
3 answers

Eager loading associations on ActiveModel instances in Rails

In RoR, it is pretty common mistake for new people to load a class and assiocations like this# the solution to eager load # The bellow generates an insane amount of queries # post has many comments # If you have 10 posts with 5 comments each # this…
austinbv
  • 9,297
  • 6
  • 50
  • 82
13
votes
2 answers

The expression 'x.Taken' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'

class Taak : [Key] public int TaakId { get; set; } public int Pid { get; private set; } [Required] [Range(0,23.5)] public double Uur { get; private set; } [Required] public…
akhandafm
  • 143
  • 1
  • 2
  • 9
13
votes
3 answers

How do you do eager loading with limits?

In the documentation for eager loading it is stated that: If you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects: class Picture < ActiveRecord::Base has_many…
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133