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 4 and mongoid reduce number of queries made

I have a Category model that has_many :items. The items have a deleted boolean field that checks if the item has been deleted or not. When listing all the categories, I also want to print the count of deleted: false items in that category This is…
Lordking
  • 1,413
  • 1
  • 13
  • 31
0
votes
1 answer

Efficiently selecting a tree in Linq-To-Sql

I have a linq-to-sql data layer, with 2 tables in it. "Parent" and "Child". There's a relationship between Child and Parent (so parent has many children, etc etc), a parent can also have many parents (when children grow up and themselves become…
Paul
  • 9,409
  • 13
  • 64
  • 113
0
votes
4 answers

Hibernate eager loading at runtime

I have already implemented java web application (core application) which gives ability to plug modules into the core application. I'm getting org.hibernate.LazyInitializationException could not initialize proxy - no Session exception while trying to…
0
votes
1 answer

Return specific data with eager load nested relationships in Laravel

Im working with Laravel 4 with MySQL and using relationships methods to access data of related tables. I have the following related tables: team table: id INT partnet_id INT -- Related many-to-one with partner table name VARCHAR(50) partner…
Darwing
  • 833
  • 1
  • 12
  • 23
0
votes
1 answer

Eager loading only first page of results with active record

I am working on a feature to search for items. My model is this, roughtly: User has many items User has many addresses User has one profile Item has many images Item has many addresses Item belongs to category I want to display the results grouped…
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
0
votes
3 answers

Grails GORM Domain class relationship

Grails 1.1.1 Goovy 1.5.7 In a relationship such this: Author 1 -- n Book n -- 1 Publisher Defined in Grails: class Author { String firstName String lastName static hasMany = [books: Book] static constraints = { …
Ricardo García
  • 325
  • 3
  • 14
0
votes
1 answer

Can I configure an entity to eager load a relation using an EntityTypeConfiguration object?

I have table Employer and EmployerParam. Almost needless to say 1 Employer has 1 to many EmployeeParams. I want that every time an Employee is loaded, its EmployeeParams are included, i.e. eager-loaded. The only other was I can think of is using a…
ProfK
  • 49,207
  • 121
  • 399
  • 775
0
votes
0 answers

When I try to do eager loading I get an error of null collection

I try to do the eager loading in this way: lstResultado = miContexto.Videos .Include(v => v.Series.Select(s => s.Episodios)) .Include(v=> v.VideosVersiones) …
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
0
votes
1 answer

Eager Loading on tracked items?

I have an element bound to an entity (Contact) that exposes some navigation properties. I want, that on some action (i.e. a "Load children" button), the Contact should load for all its children and grand children like I can do with an…
0
votes
1 answer

db.Entry.Collection.Query is not eagerly loading all virtual attributes

I have the following scenario (combined in one line) var User = db.Entry(obj).Collection(collection).Query().Where(/*some condition*/).FirstOrDefault(/*some condition*/); lets assume that User Object has Posts as a virtual attribute (to eagerly…
Hilmi
  • 3,411
  • 6
  • 27
  • 55
0
votes
0 answers

Eager loading backreferences in ActiveRecord in Rails

Apparently in my Rails application ActiveRecord is running more SQL queries than I would like. I have a simple one-to-many relation. For each package… class Package < ActiveRecord::Base has_many :screenshots end …there are several…
0
votes
2 answers

how to use eager loading without string for related entities?

I would like to use eager loading for load related entities and I see this page: In this example I can see that there are two ways to get the related entities: var princesses1 = context.Princesses .Include(p => p.Unicorns) …
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
0
votes
2 answers

How to decide between EAGER and LAZY loading in Hibernate

In hibernate or OpenJPA, if I do FetchType.EAGER, I risk loading unnecessary data and hurting performance. If I do FetchType.LAZYloading, I risk running into N + 1 problem. Are there any guidelines what fetch mode to use when?
Victor
  • 16,609
  • 71
  • 229
  • 409
0
votes
2 answers

Does LAZY fetch type produce N +1 select

After reading this article enter link description here I got the idea that a fetch mode = LAZY will always result in N +1 problem. IS this true? If so ,why? OpenJPA/Hibernate should be able to optimize the query even if fetch type is LAZY. No?
Victor
  • 16,609
  • 71
  • 229
  • 409
0
votes
1 answer

How can I do this Eager Loading (using the .Include() method) in this code?

I have a very simple repository I'm playing around with, using Entity Framework v4 that comes with VS2010 Beta 2. I'm trying to dynamically include the Include method, if a user optionally asks for it. eg. Public IQueryable GetFoos(bool…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647