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

Laravel dynamic relationships - access model attributes on eager load

I have an Eloquent relationship on my Laravel model which is dynamic - that is, the value of a particular database field determines which model will get loaded. I am able to load this relationship fine when I first instantiate the model instance and…
Andy Noelker
  • 10,949
  • 6
  • 35
  • 47
5
votes
1 answer

Rails 5 Beta: Put eager_load_paths config on initializer throw frozen array exception

I followed commented guidelines in config/application.rb which is # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in…
Yana Agun Siswanto
  • 1,932
  • 18
  • 30
5
votes
1 answer

NHibernate Eager Loading Collections + Paging

Here is an example of my entities that I am trying to return with eager loaded collections. Mixes -> Tracks (collection) -> Tags (collection) I need to return a paged list of Mixes with eager loaded tracks & tags, without paging it is relativly…
Paul Hinett
  • 1,951
  • 2
  • 26
  • 40
5
votes
1 answer

Laravel 4 Eager Loading constraints

I want to get all Items (topics) WITH their comments, if comments user_id = $id. I try something like this, but it isn't working. So if Item hasn't got any comment with user_id = $id, then I don't need this Item. In DiscussionsItem model I have…
user3921996
  • 149
  • 1
  • 1
  • 9
5
votes
2 answers

How do I do eager loading for multiple associations?

I am using public_activity and one of my partials I have this call: <%= link_to "#{activity.trackable.user.name} " + "commented on " + "#{activity.trackable.node.name}", node_path(activity.trackable.node.family_tree_id,activity.trackable.node)…
5
votes
1 answer

laravel eloquent eager loading nested condition

I have 2 tables that is using eager loading and then using nested condition in that eager loading: //migration for lead table public function up() { Schema::create('leads', function(Blueprint $table) { $table->engine = 'InnoDB'; …
rfpdl
  • 956
  • 1
  • 11
  • 35
5
votes
1 answer

Injecting @EJB in OmniFaces @Eager bean causes "Severe: No valid EE environment for injection of org.omnifaces.cdi.eager.EagerBeansRepository"

Using @ApplicationScoped @Named @Eager, my @EJB-injected @Stateless beans are not properly instantiated and evaluate to null. I had an @ApplicationScoped @ManagedBean(eager=true) that was used to schedule a few jobs. Some @Stateless beans were…
cghislai
  • 1,751
  • 15
  • 29
5
votes
1 answer

How to eager load roots using ancestry gem with rails?

I have an Entry model that belongs to a Category. The Category model is using ancestry so that I can nest Categories. I want to group a selection of entries by their root category. The problem I have is that in order to do this, rails executes a…
Christian
  • 19,605
  • 3
  • 54
  • 70
5
votes
2 answers

Why is EF eager loading include not working as expected?

I have this repository, public class Repository : IRepository where TEntity : class { private readonly DbContext context; private readonly DbSet dbEntitySet; public Repository(DbContext context) { …
r3plica
  • 13,017
  • 23
  • 128
  • 290
5
votes
1 answer

Doctrine2 eager loading runs multiple queries instead of 1

I'm using Symfony2 with Doctrine2 (latest versions) and have this relation defined: /** * @ORM\OneToMany(targetEntity="Field", mappedBy="event", fetch="EAGER") * @ORM\OrderBy({"name" = "ASC"}) */ protected $fields; The other side of the relation…
DoppyNL
  • 1,415
  • 1
  • 14
  • 24
5
votes
1 answer

SQLAlchemy, eager loading on object refresh

If have following ORM setup in SQLAlchemy: class Foo(Base): id = Column(Integer, primary_key=True) status = Column(String) barId = Column(Integer, ForeignKey("bar.id")) bar = relationship("Bar", lazy="joined") class Bar(Base): id…
dieterg
  • 123
  • 1
  • 9
5
votes
1 answer

entity framework - navigation property does not load

I have the following relation public partial class SharedResource : DomainEntity { public System.Guid Id { get; set; } public System.Guid VersionId { get; set; } public virtual PackageVersion PackageVersion { get; set; } // tried it…
Bick
  • 17,833
  • 52
  • 146
  • 251
5
votes
2 answers

Eager-load the whole object-graph at runtime in Hibernate

Please read on before saying anything along the lines of "specify the fetch type in the query". That's not what I'm after. I'm looking for a way to eager-load a complete object-graph (the object + all its children and all their children and so on).…
Nim
  • 631
  • 6
  • 12
5
votes
1 answer

Eager loading child and child-of-child collections in NHibernate

I've got a problem with NHibernate trying to load a small hierarchy of data. My domain model looks like: class GrandParent { int ID{get;set;} IList Parents {get; set;} } class Parent { IList Children {get; set;} } class…
Simon
  • 5,373
  • 1
  • 34
  • 46
5
votes
2 answers

Eager loading of deleted records with paranoia's default scope

I'm using the paranoia gem to "soft-delete" records. Now I need to eager load these records, some of which might have been deleted, for an associated model. Paranoia adds this default_scope to the "paranoid" model: default_scope :conditions => {…
Thilo
  • 17,565
  • 5
  • 68
  • 84