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|
     puts article.name
     article.comments.each do | comment |
           puts comment.comment
     end
   end

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|
        puts article.name
        article.comments.each do | comment |
        puts comment.comment
      end
    end

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.
eagerloading

About these ads

From → Ruby On Rails

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Sridhar: Web Unplugged!!!!

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

TechCrunch

Startup and Technology News

AVAS Technology

Creating Innovation in SMB Technology

Program.in

By Santosh Wadghule

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 WordPress.com weblog

Simple API

Enlarge your skills

Sutoprise Avenue, A SutoCom Source

Success & Satisfaction with the Cloud

Selenium

Just another blog for Selenium

Runway2Rails

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

railskey

Blog belong's 2 Pravin Mishra

MyRubyLearning

Teach me how to fish

Ozone

Software Development Bits\'n\'Bobs

Talent Flush Blog

www.talentflush.com

Good Sense

STARTUPS, MARKETING, AND HOW TO BE A BADASS CODER

Gobán Saor

A country datasmith.

railsfornovice

Just another blog for RoR Beginners

codedecoder

breaking into the unknown...

coderrr

coderrr

Follow

Get every new post delivered to your Inbox.

Join 183 other followers