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

Rails eager load association with selected attributes

Can anyone tell me if it's possible to eager load an association but only return specific attributes? I'd like to retrieve some orders with their accounts but only require the account names. Order.select([:id,…
Chris Hilton
  • 717
  • 1
  • 8
  • 13
8
votes
3 answers

How can I use Entity Framework on an object graph past a depth of 2 with MySQL Connector / NET?

Here is a confirmed bug report with Oracle: http://bugs.mysql.com/bug.php?id=67183 Situation When using an .Include chain inside of my repository, I noticed that I was getting strange results - mostly that the values queried that were being returned…
Travis J
  • 81,153
  • 41
  • 202
  • 273
7
votes
2 answers

laravel eloquent - Use without on nested eager loaded relations

I'm currently working on laravel framework and I'm stuck with some relations and eager loading issues. Situation I have three models A, B and C I have two relations A has many B B has many C By default (using the $with attribute in Model) : A…
7
votes
1 answer

Does :include work on ActiveRecord instances?

All the examples of :include for eager loading are for class-level querying. I tried it on my model instance and it still issued a bunch of queries - does it work on instance methods? #in controller @emails = person.sent_emails(:include =>…
Anthony Bishopric
  • 1,306
  • 11
  • 23
7
votes
2 answers

Completing object with its relations and avoiding unnecessary queries in sqlalchemy

I have some database structure; as most of it is irrelevant for us, i'll describe just some relevant pieces. Let's lake Item object as example: items_table = Table("invtypes", gdata_meta, Column("typeID", Integer, primary_key =…
DarkPhoenix
  • 103
  • 2
  • 7
7
votes
2 answers

SQLAlchemy eager loading recursive model

How can you write the model that it eager loads the parents and children of a certain role recursively. So not only the child of the role you are fetching now but also it's children. Do you risk ending in an infinite loop or does SQLAlchemy have the…
Silver
  • 1,075
  • 3
  • 12
  • 37
7
votes
1 answer

How do you deal with Linq to NHibernate's Fetch exception when selecting aggregates?

I'm using LINQ to NHibernate's IQueryable implementation on a asp.net mvc Grid (telerik specifically), where I know I'll need to fetch something eagerly for this particular grid. So I have query that looks something like this: var query = from s in…
Joseph
  • 25,330
  • 8
  • 76
  • 125
7
votes
2 answers

memcached as an Object store in Rails

I am using Memcached as an Object Store with my Rails application where I store search results which are User objects in memcached Now when I fetch the data out I get the Memcached Undefined Class/Module Error. I found a solution for this problem in…
Sid
  • 6,134
  • 9
  • 34
  • 57
7
votes
2 answers

Rails 4 Eager Load has_many Associations for single object

I get the benefits of using eager loading to avoid N+1 when fetching an array of objects with their associated records, but is it important to use eager loading when fetching a single record? In my case user has_many :addresses user has_many…
7
votes
6 answers

Extra queries listed by MiniProfiler

In my controller action, I included all associations needed by the view, to avoid multiple calls to the database. (I'm trying to isolate the the views layer to only render the data collected by the controller). I'v found out that the view still…
7
votes
1 answer

Are Clojure transducers eager?

In this blog entry, "CSP and transducers in JavaScript", the author states: First, we have to realise that many array (or other collection) operations like map, filter and reverse can be defined in terms of a reduce. So then we see a number of…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
7
votes
2 answers

How to alias in Laravel Eager Loading

I need to alias when I do a Laravel eager loading: $posts = Post::with(array('images as main_image' => function($query) // do not run { $query->where('number', '=', '0'); })) ->where('id', '=',…
kurtko
  • 1,978
  • 4
  • 30
  • 47
7
votes
0 answers

ActiveSupport::Dependencies.autoload_paths: How to eager load?

I'm using ActiveSupport in a sinatra project, and following the advice of some peers I've been using ActiveSupport::Dependencies.autoload_paths to ease the loading of my classes in development. Now I'm approaching deployment day, and I don't exactly…
ChuckE
  • 5,610
  • 4
  • 31
  • 59
7
votes
1 answer

Strategic Eager Loading for many-to-many relations in Datamapper?

I'm using DataMapper, an open source ORM for ruby, and I have in itch I would like to scratch. At the moment, DataMapper can use Strategic Eager Loading(SEL) for one-to-many relationships, but not many-to-many, where N+1 queries occur. I would…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
7
votes
3 answers

Eager loading of Linq to SQL Entities in a self referencing table

I have 2 related Linq to SQL questions. Please see the image below to see what my Model looks like. Question 1 I am trying to figure how to eager load the User.AddedByUser field on my User class/table. This field is generated from the relationship…
Saajid Ismail
  • 8,029
  • 11
  • 48
  • 56