Topic: How to Speed up Ruby Script Performance

Hi,
      I have written ruby script that is used to generate data in daily basis then update it
into reporting table called sku_wise_summaries.
I am having 5000 records in outlets table and 30 records in Skus table.
I wrote a ruby script that calculate the data by using some complex calculation for each outlet and sku then finally update data into sku_wise_summaries table.
Like...

date=Date.today
outlets=Outlet.find(:all)
skus=Sku.find(:all

  for outlet in outlets
    for sku in skus
     
        data=...... #do some complex calculation
       
        rec=nil
        rec=SkuWiseSummary.find_by_date_and_sku_id_and_outlet_id(date,sku.id,outlet.id)!=nil
        if rec!=nil
          rec.update_attributes(:info=>data)
        else
          SkuWiseSummary.create(:sku_id=>sku.id,:outlet_id=>outlet.id,:date=>date,:info=>data)
        end

    end
  end   
   
Problem
---------------
        I am running the above script on server with help of cron on daily basis.
It takes too much of time like more than 10 hrs.
I optimized the code as possible as i can and still i feel the performance is too bad.
If it takes too much of time , i have to face other kind of severe problems.

So i need to know the way of improving script speed/perfomance.

Could any one please give a good solution to improve the speed of my ruby script?.


Thanks in Advance
Manivannan J

Re: How to Speed up Ruby Script Performance

Have you tried to profile the code to figure out which part of this is taking up the majority of the time?

In any case, there are several things that you should look at:

1) Use native sql instead of the ActiveRecord create/update to create/update SkuWiseSummary entries. Also consider batching these together in fewer transactions.  You should see considerable speed up with this alone.

2) If your SkuWiseSummary table isn't already indexed, consider adding indexes to speed up your find operation. Although, this could cause some slowdown with creation events and will take up more db space.

Last edited by ehud93 (2009-07-24 13:00:50)

Play postseason fantasy sports at Fantasy Postseason - Written in RoR