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

Rails eager loading

HI, I have a Test model, which has_many questions, and Question, which has_many answers... When I make a query for a Test with :include => [:questions, {:questions => :answers}] ActiveRecord makes two more queries to fetch the questions and then to…
Dimitar Vouldjeff
  • 2,067
  • 3
  • 19
  • 24
0
votes
1 answer

Rails Mongoid vs ActiveModel and eager loading

I find a lot of documentation on the internet about eager loading and optimisations for ActiveRecord, not so much about mongoid. I'd like to think everything works the same, but that would be too easy, and probably totally wrong in some cases. There…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
0
votes
2 answers

Laravel Eager with Where constraint

I have 2 methods with eager loading and paginate. but problem is that i have to use where constraint too and i am getting offset 0 error. public function showAllOrders(){ $orders =…
0
votes
1 answer

Deciding type of fetch at runtime

I have an entity Application.java, which in turn has a property @Audit(changeTracker = AttributeValueListChangeTracker.class) @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true, targetEntity = AttributeValue.class) @OrderBy …
Shruti Rawat
  • 687
  • 6
  • 11
  • 24
0
votes
0 answers

Laravel eager loading issue

Say I have an orders table and a users table and a guests table, for a company who accept orders from both users and guests. I also have a is_guest boolean on the orders table which lets me know quickly whether the order has been made by a guest or…
Mr Office
  • 290
  • 1
  • 9
0
votes
1 answer

conditions in eager loading in eloquent laravel

I get my Product be this code. I use with() method to load variations relation. I have some filters for it. But if any variations does't exist I get Product. How can I get only this Product where variations exist ? $query = Product::with(array( …
Thomas Shelby
  • 1,340
  • 3
  • 20
  • 39
0
votes
1 answer

Eager loading not working - Laravel

I have eager loaded the data but when I am using $object->attribute it again fetch the data from database. My query is : $user = User::with([ 'Comment' => function($query){ $query->where('active', 1); …
0
votes
1 answer

Rails 4 eager loading (association vs method) efficiency

The following method and association accomplish the same thing. They both make an 'active_students' method available to the current school. However, the association uses 125 queries, and the method uses 1. The method uses eager-loading but, the…
hellion
  • 4,602
  • 6
  • 38
  • 77
0
votes
2 answers

Counting eloquent relations from pivot table in L4

I have the following tables: users Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username', 30); $table->string('email')->unique(); $table->string('password',…
Latheesan
  • 23,247
  • 32
  • 107
  • 201
0
votes
1 answer

Eager loading not working in L4

I have the following tables: users Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username', 30); $table->string('email')->unique(); $table->string('password',…
Latheesan
  • 23,247
  • 32
  • 107
  • 201
0
votes
1 answer

Eager load this rails association

I have rails app which has a list of users. I have different relations between users, for example worked with, friend, preferred. When listing the users i have to decide if the current user can add a specific user to his friends. -if…
dombesz
  • 7,890
  • 5
  • 38
  • 47
0
votes
1 answer

Laravel eager loading not working?

Airports Table: Schema::create('airports', function(Blueprint $table) { $table->increments('id'); $table->string('code'); $table->string('name'); $table->string('city'); …
Rohan
  • 13,308
  • 21
  • 81
  • 154
0
votes
1 answer

Convert Laravel query to Eager Loading

I've been leaning Laravel and trying to implement something and I did, it worked but I heard that it might be possible to do the same in an easier way using Eager Loading. Imagine that we have 4 tables: garages, cars, securities, places. garages is…
yayuj
  • 2,194
  • 3
  • 17
  • 29
0
votes
2 answers

Ordering on a many-to-many relationship with FuelPHP

I've got a problem with sorting on a many-to-many relationship. What I'm trying to achieve seems like it should be fairly straightforward, but after a lot of banging my head against Fuel, I still can't get it to work. (Incidentally, I realise…
Nick F
  • 9,781
  • 7
  • 75
  • 90
0
votes
2 answers

Entity Framework Load Property of Child

I have the following structure: public class A{} public Class B:A { public virtual C { get; set;} } public Class C{} public Class Context:DbContext { public DbSet As { get; set; } public DbSet Cs { get; set; } } When I want to…