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
5
votes
0 answers

Lazy loaded modules are being bundled with main chunk

I have a client's project that is using the Angular 6 framework. There are several modules in the project, 16 of which are supposed to be lazy-loaded (e.g. loadChildren). But when I run build for production, only 9 of those modules have their own…
shihab
  • 51
  • 2
5
votes
4 answers

NHibernate ThenFetchMany is retrieving duplicate children

I have a parent object with a child collection containing one element, the child collection contains a "grandchild" collection containing 3 elements. I am loading the parent object from the database using NHibernate as follows Parent parentObject =…
Simon
  • 1,499
  • 3
  • 17
  • 23
5
votes
0 answers

Laravel eager loading query where 0 = 1

when I execute this code in Laravel 7.2.0 $news = NewsPost::select('title','slug','category_id', 'image', 'fulltext', 'created_at')->with('category:id,title,slug')->orderBy('created_at', 'desc')->simplePaginate(10); I have 3 query on the…
schel4ok
  • 634
  • 1
  • 11
  • 33
5
votes
2 answers

Laravel - one-to-one relation through pivot table with eager load

I have this relationship A Movement can have multiples steps A Step can belongs to multiples Movements So a had to create a pivot table and a belongsToMany relationship, but my pivot table have some extras columns, like finished and order I want…
SpaceDogCS
  • 2,808
  • 3
  • 20
  • 49
5
votes
2 answers

Entity Framework Eager Loading Filter

I have a simple query I want to do like this: 1) Products have ChildProducts which have PriceTiers 2) I want to get all the Products that have a Category with a ID of 1 and Display = true. 3) I want to then include all the ChildProducts that have…
Sam
  • 15,336
  • 25
  • 85
  • 148
5
votes
2 answers

Rails ActiveRecord helper find method not eager loading association

I have the following models: Game and Pick. There's a one to many association between Game and Pick. There's a third model called Player, a Player has many Picks. There's a method in the Player class that finds a pick for a given game or creates a…
Vadim
  • 17,897
  • 4
  • 38
  • 62
5
votes
1 answer

Problem with Eager Loading Nested Navigation Based on Abstract Entity

I a portion of my EF model that looks like this: Summary: Location has many Posts Post is an abstract class Discussion derives from Post Discussions have many Comments Now, the query i'm trying to achieve: Get information about Location Id 1234,…
5
votes
3 answers

Laravel 5.6 Trying to get property of non-object

When i try echo the value i receive an exeception. I check the collection with dd() and is not null. My Models: Cliente:
5
votes
0 answers

MySQL Include() does not work in EF .NET Core 2

I have a MySQL database where the table Game has a FK to the table GameStats (Game.GameStatsId -> GameStats.GameStatsId), but can't get it to load when fetching Games with: var games = _context.Game.Include(g => g.GameStats).ToList(); When it logs…
boffman
  • 433
  • 1
  • 5
  • 13
5
votes
1 answer

Generic repository with eager load using ThenInclude() method?

Here is my generic repository: public class Repository : IRepository where T : BaseEntity { private DbContext _dbContext { get; set; } private DbSet _dbSet { get; set; } public Repository(DbContext context) { …
Tan Sang
  • 1,897
  • 1
  • 16
  • 28
5
votes
2 answers

laravel hydrateRaw/fromQuery and eager loading with pagination

I currently found out that you can hydrate an Raw sql query. I have following query: DB::table(DB::raw('(SELECT *, Y(location) AS longitude, X(location) AS latitude FROM meetings WHERE MBRCONTAINS(@quadrat, location)) AS sub')) …
Ronon
  • 738
  • 10
  • 26
5
votes
1 answer

Doctrine fetch EAGER in many-to-many association

Using Symfony 3.2 and Doctrine 2.5, I have trouble to understand how fetch "EAGER" should work in many-to-many and many-to-one relationship. Lets say we have two entities, User and Site, and a many-to-many association: class User { /** *…
lcp
  • 51
  • 1
  • 5
5
votes
2 answers

Using eager loading with specification pattern

I've implemented the specification pattern with Linq as outlined here https://www.packtpub.com/article/nhibernate-3-using-linq-specifications-data-access-layer I now want to add the ability to eager load and am unsure about the best way to go about…
5
votes
1 answer

EF4 LINQ Ordering Parent and all child collections with Eager Loading (.Include())

I am doing hierarchical data binding on a grid, and I need to have the database server perform the sorting on my objects. I can easily sort the parent collection, but I can't seem to figure out how to also sort all the child collections. I have a…
Travis
  • 293
  • 1
  • 5
  • 13
5
votes
2 answers

Laravel belongsTo with condition and eager load

I have a Post model associated to a Section model, which depend on an extra condition to work: belongsTo('App\Models\Section', 'id_cat')->where('website',…
lkartono
  • 2,323
  • 4
  • 29
  • 47