Thursday, December 4, 2008

Import data from CSV or Excel in to database

Simple Way To Import CSV/Excel data.

If u have excel file then save it as CSV file.
Consider a table foo and it has two columns (name,status)
Put ur CSV file in config folder
Create a migration file.. Put the below code in ur self.up method

def self.up
filename = "#{RAILS_ROOT}/config/Datafile.csv"
file=File.new(filename,"r")
while (line = file.gets)
c = Foo.new
columns = line.split(",")
c.name=columns[1]
c.status=columns[2]
c.save
end

Run the migration using rake command 'rake db:migrate --trace'
and thats it..!! You Got Ur Table Loaded With Data u Need..!!

Cheers..!!

Wednesday, December 3, 2008

How to Improve Performace in Rails Application

1.Do not use dynamic finders like find_by_name.. find_by_columnname..
This will internally call a method named 'method_missing' in activerecord then it will construct a query and execute it.

2.Do not use rails helpers like link_to

3.Avoid associations

4.Avoid too many before_filters

5.While writing query for eg. Model.find(:all)
try to use select in this so that u limit only the required rows from the database

6.Move your javascript and css to the bottom of ur page..
<%=javascript_include_tag "prototype"%>

7.If u r using n-1 relation then it can be optimized thru piggy back technique http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes

8.Also do indexing in your database.

What is Indexing and why Indexing?
In a database, you have a few different kinds of objects. You’ve got tables, which are just big containers in which you put your data. The data is not sorted, so if you want to find something in a table, you need to look through everything until you find what you want.

You also have indexes. An index is a sorted list of rows in a table. It contains a small subset of the table’s data, just enough to sort the data and point to the real data rows in the real table. So, when you want to find something in a table, you look at a suitable index, not the table. The index will point to the rows of the table that match what you are looking for.


9.Monitor ur rails app performance using tools like RPM,Production Log Analyzer (http://rails-analyzer.rubyforge.org/pl_analyze/),clientperf(http://austinentrepreneur.wordpress.com/2008/06/21/announcing-clientperf-simple-client-side-rails-performance/)

10.Also check for any memory leak in ur application since ruby is prone to it.