Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eager loading and lazy loading in rails

I am confused about eager loading and lazy loading, is there any difference in the performance of rails queries?

Is there any way to implement both ways?

like image 302
Arpit Vaishnav Avatar asked Apr 10 '12 06:04

Arpit Vaishnav


People also ask

What is difference between lazy loading and eager loading?

While lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed. Eager loading also involves pre-loading related entities referenced by a resource.

What is eager loading and lazy loading in Ruby?

1) One for all users. 2) One for all friends of users . Lazy Loading : When you have an object associated with many objects like a User has many Friends and you want to display a list as in Orkut you fire as many queries as there are friends, plus one for the object itself. users = User.find(:all)

What is eager loading rails?

Eager loading solves this problem by creating a left outer join on the table, returning all of the data in a single query. Adding an eager load is as simple as adding an :includes statement to the controller.

What is lazy loading in Ruby?

So by default in Ruby on Rails, the ORM has lazy loading enabled, which means it delays the loading of data until the point where we actually need it. In our case, first it's the controller where it's asked to fetch all the posts. 1.


2 Answers

Eager Loading

One way to improve performance is to cut down on the number of SQL queries. You can do this through eager loading.

User.find(:all, :include => :friends) 

Here you are firing only two queries :

1) One for all users.

2) One for all friends of users .

Lazy Loading :

When you have an object associated with many objects like a User has many Friends and you want to display a list as in Orkut you fire as many queries as there are friends, plus one for the object itself.

users = User.find(:all) 

Then query for each user friend , like :

users.each do |user|   friend = Friend.find_by_user_id(user.id) end 

Here

1) One query for all users.

2) N query for N no. of users friends .

Take a look on : Rails 3: Lazy loading versus eager loading

Hope that will help you to understand this .

like image 130
Vik Avatar answered Sep 20 '22 16:09

Vik


Eager loading

Pro: is that everything's ready to go.

Con: you are using up space/memory.

Lazy Loading

An anecdote might help you remember:

A young naval cadet asked Lord Nelson why he wasn't preparing his ships: 

"I won't load my guns early.......I'll load just 1 microsecond before I need to fire it." he said. "I'm being lazy. I prefer doing it at the last minute, just like with my university assignments etc."

Pro of lazy loading: you don't hit the database until you need to.

Con: You'll be hitting the database N + 1 times.....unless you select exactly the column you want and you alias it. e.g.

@products = Product.order("categories.name").joins(:category) 

Hitting the Database only once with a lazy loading policy:

The above query hits the database N + 1 times when you call product.category.name in the view template - where product is a single object within the @products relation. But if you alias it, you can get everything done with just one query:

@products = Product.order("categories.name").joins(:category).select("products.*, categories.name as category_name") 

And use it like this: product.category_name

like image 38
BenKoshy Avatar answered Sep 24 '22 16:09

BenKoshy