Top Ten Ways to Speed Up Your ROR Application: Session Storage: Choose your session storage carefully according to your need. Here are what rails provide: CookieStore– Stores everything on the client. DRbStore– Stores the data on a DRb server. MemCacheStore – Stores the data in a memcache. ActiveRecordStore – Stores the data in a database using Active Record. •DRY (Don’t repeat yourself ): Programmers tend to listen and don’t follow this basic principle. e.g: Novice Way
if(Student.find_by_id(params[:id]).name == " Santosh") return Player.find_by_id(params[:id]) else return nil endExperienced Way :
student = Student.find_by_id(params[:id]) if(student.name == "Santosh") ? student : nilSTI :Use STI(Single Table Inheritance) wherever needed which Dries up your Model. Single Table Inheritance is, as the name suggests it, a way to add inheritance to your models. STI lets you save different models inheriting from the same model inside a single table. For example, let’s say you have an employee model. The employees can be of two types : manager or developer. They pretty much share the same attributes and columns. However, their behavior should be different. Creating two tables having the exact same fields would be bad. Slim/Thin Controller: Thin controllers are easy to test and has good performance profile because there’s some overhead involved in passing the controller instance variable around. In short, you need to follow “Thin controller and slim model . Use Of Partials : Use of partials help to cache your web pages content/component easily. You can also thread your views to render data's parallely. Eager loading : Eager loading is a way to solve the classic N + 1 query performance problem caused by inefficient use of child objects. Have a look at the following Code: It will fetch account of 100 users.
users = User.all(:limit => 100) users.each do |user| puts user.bank_details.accounts endHence, 101 queries will be executed, 1 for the top and 100 for rest. The solution is to rewrite it to eager load accounts.