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

Eager loading a tree in NHibernate

I have a problem trying to load a tree, this is my case, I have an entity associated with itself (Hierarchic) with n levels; the question is, Can I load eagerly the entire tree using ICriteria or HQL? Thanks in advance for any help. Ariel
Argons
  • 445
  • 1
  • 6
  • 23
4
votes
2 answers

How to Eager Load Associations without duplication in NHibernate?

I'd need to load a list of very large objects with so many children and children of children. what's the best approach to take? I'm using Oracle 11g database and I've written the below method but it results in cartesian product (duplicated…
The Light
  • 26,341
  • 62
  • 176
  • 258
4
votes
1 answer

How to eager load a nested polymorphic association

I'm trying to eager load a nested polymorphic association. I can't seem to find a solution. Here is my model setup: class Post has_many :comments end class Comment belongs_to :ownable, polymorphic: true end class User has_many :comments,…
axsuul
  • 7,370
  • 9
  • 54
  • 71
4
votes
1 answer

Entity Framework 5 eager loading with parent properties

I have a query on some parent entity (Order) and I want to load some of its sub collections or properties eagerly. I have a query like this: public void QueryMethod() { using (var context = new MyContext()) { var orders =…
ayk
  • 1,407
  • 2
  • 19
  • 26
4
votes
4 answers

Loading data from associated model in same query in rails

Basically, I have a Listing model, where each listing has a country id. I need the country name in my search results view. I know I can do @listing.country.name, but this performs an extra query for each listing in my search results. I'm using…
Dave
  • 1,051
  • 1
  • 10
  • 20
4
votes
1 answer

Eager load Records in Orchard CMS

I'm building an Orchard CMS module, where I want to eager load data, but can't work out how to do this. For example a Client has many Events, so I have a ClientRecord & EventRecord for these: public class ClientRecord { private…
Appetere
  • 6,003
  • 7
  • 35
  • 46
4
votes
1 answer

Eager Loading multiple relationships with a filter on a relation with Eloquent ORM

I have the following code that doesn't work: $threads = MessageThread::with('last_message', 'thread_visibility') ->where('message_thread_visibility.user_id', Auth::user()->id)->get(); What is the best way to add a "where" clause to an eagerly…
eski009
  • 371
  • 1
  • 6
  • 14
4
votes
2 answers

How to always include a model for eager loading

I use the bullet gem to let me know of N+1 queries. I want to avoid adding include sporadically. I have a comment model which belongs to a user model Is there a way to tell the model that anytime a comment model is being accessed to include the user…
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
4
votes
1 answer

Eager loading relation when relation is defined by finder_sql - ignores finder_sql and build default has_many query

There is class called Location. I would like to to preload all direct children within a single query. In Location class relation is defined like: has_many :children, class_name: self, finder_sql: ->(query) { …
4
votes
1 answer

Lazy.. but eager data loader in F#

Does anyone know of 'prior art' regarding the following subject : I have data that take some decent time to load. they are historical level for various stocks. I would like to preload them somehow, to avoid the latency when using my app However,…
nicolas
  • 9,549
  • 3
  • 39
  • 83
4
votes
1 answer

How do get Entity Framework ICollection loaded by default, with no lazy loading?

I am using entity framework code first and I want to mark a collection for use no lazy loading. I dont know this concept are called as eager loading. But so far I know I just have to set virtual attribute for using lazy loading. But if I dont want…
iuristona
  • 927
  • 13
  • 30
4
votes
1 answer

Entity Framework - "eager loading" using .Include() and .Select() - how to use a LEFT JOIN rather than INNER JOIN?

Here is a query I'm working on in Entity Framework 5.0.0 RC (code first) with .NET 4.0 I'm new to Entity Framework, so I'm still getting my head around how to structure the queries, particularly around selecting "child" related data. I'm using…
asgeo1
  • 9,028
  • 6
  • 63
  • 85
4
votes
2 answers

Dynamically eager-loading STI classes

Scenario: I have a number of STI models in my Rails 3.2 app. At times, I use parent classes to query the databases for child classes, as shown below: class ParentObject < ActiveRecord::Base end class ChildObject < ParentObject end class User <…
Henrique Zambon
  • 1,301
  • 1
  • 11
  • 20
3
votes
1 answer

Eager Loading aggregate roots with Entity Framework

I would like to create a more structured approach to loading the needed entity-tree: I need a serious amount of data, so I'm doing this using type-safe Includes (just a normal Include but with Lambda's) as shown here. As I said, I need a lot of…
Bertvan
  • 4,943
  • 5
  • 40
  • 61
3
votes
1 answer

.preload using two databases

My Rails 3.2 app use two different databases. The main os is a MySQL while the oher is a sqlserver DB : development: adapter: mysql2 encoding: utf8 database: mydb username: myuser password: *** secondbase: adapter: sqlserver …
LapinLove404
  • 1,939
  • 1
  • 21
  • 26