I have a model that is sorted in a particular order. My goal is to find a record from the model where the sum of a particular column of all previous records equals a certain number.
The following example gets me what I need, but it is very slow, especially with a rather large table. Are there any faster ways to solve for the product.id where the sum of all previous products’ points = 100000?
total_points = 0
find_point_level = 100000
@products = Product.order("id").all
@products.each do |product|
total_points = product.points + total_points
@find_product = product.id
break if total_points >= find_point_level
end
Update
Here are some times for a few of the solutions below. This is going through about 60,000 records. Times are for ActiveRecord.
Original example (above):
2685.0ms
1238.8ms
1428.0ms
Original example using find_each:
799.6ms
799.4ms
797.8ms
Creating a new column with the sums:
181.3ms
170.7ms
172.2ms
You can try denormalizing your database, and keeping partial sums directly in
productstable. Simple query withwhereandlimitwould return you a proper answer in no time.You need to create additional filter, that will update single record whenever product is added, and all products whenever product is deleted or it’s
pointsfield were changed.