Photo by Thanos Pal on Unsplash

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

Mayurkumar Patel
2 min readOct 1, 2020

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.

Benchmark.benchmark do |x|
x.report("SQL Sum:") { Nutrient.sum(:value) }
x.report("Ruby Sum:") { Nutrient.sum(&:value) }
end
SQL Sum: 0.000000 0.000000 0.000000 ( 0.123737) # ~57x faster
Ruby Sum: 6.700000 0.010000 6.710000 ( 7.067941)

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.

Benchmark.benchmark do |x|
x.report("SQL Max:") { Nutrient.maximum(:value) }
x.report("Ruby Max:") { Nutrient.pluck(:value).max }
end
SQL Max: 0.020000 0.010000 0.030000 (0.266164) # ~25x faster
Ruby Max: 6.180000 0.100000 6.280000 (6.628453)

Example #3 Minimum

Benchmark.benchmark do |x|
x.report("SQL Max:") { Nutrient.minimum(:value) }
x.report("Ruby Max:") { Nutrient.pluck(:value).min }
end
SQL Min: 0.000000 0.000000 0.000000 (0.131808) # ~53x faster
Ruby Min: 6.760000 0.010000 6.770000 (6.978912)

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

--

--

Mayurkumar Patel

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