Developing a application and optimizing its performance should go hand in hand but mostly the due to achieve the deadlines and complete the features in a project the scope of optimization reduces and the optimization is kept for the end (which is not a good practice) and other factor such as lack of experience and substandard coding also lead to decrease in the performance.
The main areas to concentrate to improve you app performance while developing :
- Database optimization and Query optimization
Lets concentrate on database optimization and here are few quick tips (again these are basic tweaks which we feel should be applied & are subjective to applications and developers)
- Maintaining proper indexing for required tables in the db (don’t over do indexing ,it may also lead to lowering performance. You can decide this on case to case basis)
- Maintaining proper relation and association between models also is major factor effecting the app performance and proper utilization of association will increase the performance.
- Fetch only when it is required and only what is required and reuse the fetched data from the db as much as possible.
- Optimize the query by limiting the data fetched and fetch data in batches for large amount of data.
- Database caching can be used to reduce the response time and number of queries . We can achieve it by implementing memcached and dalli gem.
- Do not write queries in loop, Its the biggest Don’t do while coding. .If its already done find a way to rewrite that part of code and avoid the calling query in a loop situation.
These are the few point can be looked in to for optimizing the rails application. We would also recommend Bullet gem in development which is very useful to reduce N+1 queries in application .
The project is available on GitHub: http://github.com/flyerhzm/bullet
If you have any other pointers to add to this, feel free to comment. We shall take them while writing our next set of articles.