Image for post
Image for post
Photo by Thanos Pal on Unsplash

Apexit #04: How to increase the performance of your Rails application? (Part 1)

Apexit is technical articles series in which I will take one real-world problem, will discuss it, develop code for it if needed and share the source code with other people.

Today we will discuss how ActiveRecord::Calculations will come to rescue when you have a performance issue. One of my clients had a performance problem with his application. After reviewing his application code, I found that operations on data were the major source of time consumption. Certainly, there are other reasons as well but database querries were dominating. I had fixed his application performance issue by implementing ActiveRecord::Calculations module properly. Let’s see some of the examples.

Example #1 Sum

There are more than 700,0000 records in the nutrients table. The application was taking the sum of all nutrient values by Enumerable#sum module. This is very inefficient. I solved this problem by using ActiveRecord::Calculations.

Example #2 Maximum

The following two examples were not a problem with my client’s application but for curiosity, I have compared the results. Clearly, ActiveRecord::Calculations has much faster performance.

Example #3 Minimum

If you are facing any performance issue with your application then, please feel free to drop a comment. I will be very happy to answer and share with others. Also, you can book an appointment with me here: https://mayurkumar.info

Call To Action

I am running my two online courses. Join my master 4-week course on “Ignorance of Software Developer and How we can reduce its impacthere: https://forms.gle/ 8RWe119KgGo8mBHF6. If you are short on time then join my short course for “Practical Tips for Ruby on Rails” here: https://forms.gle/DLKSdhb7xardVreE6. This course is for all the levels of programming. You will receive a PDF in your inbox once a week. It’s absolutely FREE!! Sign Up.

About Author

Mayurkumar is helping companies to take the stress out of software development and make their business shine. He has more than eight years of experience in designing and building scalable applications using different technologies.

Thank you for reading my article. Read more at https://mayurkumar.info

Written by

helping companies to take the stress out of software development and make their business shine.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store