Skip to content

EagerLoading and LazyLoading

March 21, 2013

Eager loading and lazy loading

1. Eagerloading loads all the records of the objects and also associated records of the objects. It is mainly useful to improve the performance. It will take less time to load all the required records.

Let us go with an example

Assume the simple scenario we have 2 models “Article” and “Comment“. The relation between these two models are one – many

Article model has the relation
has_many :comments
Comment model has the relation
  belongs_to :article

So, if we need to get all the articles list what generally we can do is

simple following the below process

articles = Article.all

If we need only few

  articles = Article.all(:limit => 5)

Now let us get all the articles and comments list of that particular articles

what generally we can do is

   articles.each do |article|
     article.comments.each do | comment |
           puts comment.comment

The above code is perfectly good while looking in to the logic
It provides the output what we are expecting .
But when we look over in to performance .We can find the performance issue. taking lot of time if we have thousands of records

Why it is taking time?
The answer for the above question from our example is
it fires only query to find all the articles but it fires N queries to find all the comments for the article.

so it is taking much time.The above procedure to get all the articles and comments is comes under lazy loading
You can check the performance time using benchmark to test howmuch time the query is taking to get results

How to avoid this performance issue?

Using Eagerloading we can solve this issue. We can provide includes method provided by Active Record so it is useful and helps to loads the full objects and the associated records of the objects.

lets try to get the list  of articles and comments using include method

        articles = Article.includes(:comments)
      articles.each do |article|
        article.comments.each do | comment |
        puts comment.comment

Now we have only 2 queries to fire , one to find all the articles and another one is to find the comments for the articles.
This is called eagerloading by using this way we can overcome the performance issue.Now you can check the performance of this method using benhmark to test howmuch time it is taking to get all the results.
So that you can get a cleat idea about eagerloading and lazyloading.

It is always better to use instead of lazyloading.


From → Ruby On Rails

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


My discoveries....

Sridhar: Web Unplugged!!!!

Thank god. It Isnt a Brain Surgery, Its a Web App!!!

AVAS Technology

Creating Innovation in SMB Technology

Suman Srinivasan's Code Blog

Code samples and tutorials during my work/school

Wei's world of Linux/Ubuntu+KDE

Better Life Experience with Linux

Official Selenium Blog

Just another weblog

Simple API

Enlarge your skills

SutoCom Solutions

Success & Satisfaction with the Cloud


Just another blog for Selenium


Ruby on Rails runway....................


Teach me how to fish


Software Development Bits\'n\'Bobs

Talent Flush Blog

Good Sense


Gobán Saor

A country datasmith.


Just another blog for RoR Beginners


breaking into the unknown...

Talking Experience

Life is a journey where choices are made every moment

Glenn's Thoughts

Web Development In My Perspective