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

Laravel 4 eager loading and categories, subcategories, articles

Hi i thought i can handle this myself, but actually i don't know how to bite it. I am trying to categorise my programs. There will be only 2 levels of categories: 1 CATEGORY 2 |-Subcategory I want it to be as simple as possible. - program can belong…
SONGE
  • 27
  • 1
  • 9
0
votes
2 answers

Mongoid: eager loading relations in parent and its subclasses

I'm using subclasses in mongoid and I would like to eager load relations of both the parent and its subclasses. class Event include Mongoid::Document belongs_to :modifier end class Fixture < Event belongs_to :club end When I run…
leg100
  • 1
  • 2
0
votes
1 answer

efficiently traverse all users and return 3 associated items [SQL] [Rails]

I'm running rails 4. We have a User model has_many Posts. I would like to create a 'feed' that displays a maximum of three posts from every user in the site. the closest I could get was something along the lines of # GET /users # GET…
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
0
votes
1 answer

EF Eager Load - How to join deeply across several tables?

I have a well-normalized database and am having trouble crafting an EF query to perform a select joining together enough information from several different tables at once. I realize I could issue separate queries to pull in the associated data,…
0
votes
2 answers

Using Nested Intermediate Tables

I'm working with Laravel 4 and trying to setup a table structure for handling the following problem. I have 3 tables: Players, Teams & Seasons For each season, I will have multiple teams assigned and each team will have multiple players…
Jeremy
  • 179
  • 4
  • 13
0
votes
0 answers

Eager loading in Hibernate doesnt work, referenced attrribute has null values

i have a hibernate problem, probably due to lazy/eager loading or something similiar. I have a model class student and a model class century. The student has an attribute century. Before I save the stdent in the dao i save the referenced century.…
olkoza
  • 715
  • 2
  • 17
  • 35
0
votes
1 answer

Eager loading VS Lazy loading in a n+1 situation

How to deal to have a good performance with complex data query like this : In my Data access layer : public IEnumerable Search(SearchCriteria searchCriteria) { //Operation to have my predicate... return…
0
votes
0 answers

Load properties of a type without other properties of a type?

Is it possible to eager load one property of a type without loading all it's properties? Real life situation: I have two entities, MediaLink and MediaItem. My media item looks like this: public partial class MediaItem { public MediaItem() { …
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
0
votes
1 answer

Rails has_many relationships when not saved (possibly ajax related)

Have a quick question about the has_many and belongs to. I'm in a situation where a :user has_many :accounts, and an :account belongs_to the user. In my controller, I first assign @accounts = current_user.accounts. Debugging [correctly] reports to…
Josiah
  • 114
  • 11
0
votes
1 answer

Entity Framework Eager Loading Multi Levels, Without String

This is one way to do Eager Loading: dim Q = from o in contex.Orders.Include("Items").Include("Items.Products") I want to do that without using Strings. With one level it's easy: dim Q = from o in contex.Orders.Include(Function(x) x.Items) But how…
0
votes
2 answers

How do I set the Fetchmode on all assocations of a nHibernate ICriteria in one go?

I have an object that has many assocations to other objects. All of these are fetched lazily by nHibernate, which is good in almost all cases. In a particular scenario, in this case an export of a lot of records, I want to set the Fetchmode to…
Ruben Steins
  • 2,782
  • 4
  • 27
  • 48
0
votes
1 answer

Laravel many to many eager loading always returns empty results

I am trying to fetch all classes of Entry with some condition on it's attributes. One relationship, tags, should be eager loaded. The models looks like so class Tag extends Eloquent { protected $table = 'tags'; protected $guarded = array(); …
C-A
  • 699
  • 1
  • 6
  • 11
0
votes
0 answers

Entity Framework 5 - Is it possible to load relationships (associations) without lazy and eager loading?

I'm trying to develop a generic repository on Entity Framework 5 for get data from any table and I want to use disconnected context without lazy and eager loading. Is it possible? I'm using the code below, but it doesn't works: T it's entity type.…
0
votes
1 answer

Rails active record eager loading won't load associations

I'm writing a REST api to return tasks lists. Here's my set of data: id | name | parent_id 1 | Ride a horse | 0 2 | Eat tacos | 0 3 | Get some cash| 2 A Task can have subtasks so I made my model self joined: class Task <…
gkpo
  • 2,623
  • 2
  • 28
  • 47
0
votes
2 answers

Preferred way to convert from Lazy/Delay-loading to Eager-loading in an API?

I've been working on an API (which wraps a web-service of sorts) for a while now, and its just about feature complete. I initially designed this API to be lazy/delay-loaded throughout; which makes perfect sense if you're only interested in a small…
Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137