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

Entity Graph ignored when loading related entities

I have an entity graph defined in my Person entity. When I declare the phone attribute as an attribute node, a join clause is created and only one select is executed, as expected. But when I remove the phone attribute from the entity graph, the…
dinhokz
  • 895
  • 15
  • 36
4
votes
1 answer

Eager Loading with Eloquent

I have the following code: $users = User::with('profile')->paginate($count); and: $roles = Role::withCount('users')->get(); Now using laravel-debugbar and when I open my users page what I see is that 107 queries are being executed making the page…
user3718908x100
  • 7,939
  • 15
  • 64
  • 123
4
votes
1 answer

EntityFramework: Eager loading with excludes instead of includes?

My data model has a lot of nested entities and I would like to eagerly load the whole object tree ... except for a view entities that will be explicitly loaded on demand. Using include paths I have to specify many paths and every time I add a new…
Stefan
  • 10,010
  • 7
  • 61
  • 117
4
votes
1 answer

How to use eager loading with custom query builder in Laravel

I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below: $view = DB::table('blocks') ->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id') …
Dhirender
  • 604
  • 8
  • 23
4
votes
2 answers

Force lazy loading of usually eager attributes

How do I force a specific query to lazy load an attribute, that usually has eager loading? EntityGraphType.FETCH isn't working. @NamedEntityGraph(name="negGraphName",attributeNodes={ @NamedAttributeNode("name"), …
f.khantsis
  • 3,256
  • 5
  • 50
  • 67
4
votes
0 answers

Eager Loading using EF6 Fluent Api with a Composite Key Fails to Load Related Entity

I must be creating the composite key incorrectly or connecting it to the related entity incorrectly. I can get it to work in a simplified scenario of a single key, but the composite key fails. I'll show both working and non working versions. My…
4
votes
1 answer

EF eagerly loading Navigation Properties issue

I am using EF6 with Generic Repository pattern. Recently I experienced a problem trying to delete a composite entity in a single go. Here is a simplified scenario: public class Parent { public int Id { get; set; } public string Name { get;…
4
votes
1 answer

Entity Framework 4 Abstract Model - How to Programatically Eager-Load Navigational Properties?

I have an EF4 Model that is built with abstract entities/classes: Notice how State entity has a navigational property called Country. Note: I have lazy-loading disabled, so i must eager-load on demand. Now, if i have the following method: public…
RPM1984
  • 72,246
  • 58
  • 225
  • 350
4
votes
2 answers

Laravel: How to get selected columns of Base Table with Eager Loading?

For example here the table structure. User ----- -id -role_id -fname -lname -country_code -mobile -password -remember_token -email -address -location_id And Another table Role ----- -id -name Now if I want all user information from user table…
Kavan Pancholi
  • 601
  • 5
  • 15
4
votes
1 answer

Symfony/doctrine: Optimize n+1 queries

Context Imagine a set of proposals, where every user can vote up or down for a proposal. So I have 2 models: A Proposal, A Vote, related to a proposal, a user, and an opinion (up or down). Now I want to display all my proposals, including extra…
pierallard
  • 3,326
  • 3
  • 21
  • 48
4
votes
0 answers

Handling eager loading and unsaved associated records

How can I handle a scenario where associated records may not be saved to the database? If I have class Blog < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base has_many :comments end then adding an includes statement for…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
4
votes
1 answer

Can I eager load a property using HQL?

I'm trying to work out how to eager load the customers in the following HQL query: select order.Customer from Order as order where order.Id in ( select itemId from BadItem as badItem where (badItemType = :itemType) and (badItem.Date >=…
dommer
  • 19,610
  • 14
  • 75
  • 137
4
votes
3 answers

Doctrine fetch join fails to hydrate correctly

Original question I am doing a fetch join in Doctrine, using a join table that has a composite key and no other defined fields, and getting incorrect data loaded into my entity. Is this a bug in Doctrine, or am I doing something wrong? What follows…
jake stayman
  • 1,687
  • 13
  • 22
4
votes
1 answer

Hibernate: Unable to eagerly fetch child collection of a child collection

I have a Session class, which has a mapped collection of Photo objects: @OneToMany(fetch = FetchType.EAGER) @Fetch(FetchMode.SUBSELECT) @JoinColumn(name = "sessionId", insertable = false, updatable = false) private SortedSet photos = new…
rweiser
  • 319
  • 1
  • 3
  • 13
4
votes
1 answer

Doctrine Class Table Inheritance - Eager loading sub-class references

Consider the following diagram: Where a User has many BlogPost's, an Event is a type of BlogPost with a property of Venue. I have the following entity classes: ** * @ORM\Entity() * @ORM\Table(name="User") */ class User { ... /** *…
Rob Forrest
  • 7,329
  • 7
  • 52
  • 69