Currently I am running something similar to this command:
Person.sum(:cholesterol, :group => :age)
which seems to work great when I have a small number of records. However when I attempt to call this with a few thousand records or more in the table, it takes literally minutes to run. However, the SQL query:
SELECT sum(`people`.cholesterol) AS sum_cholesterol, age AS age
FROM `people`
GROUP BY age
Takes around 0.02 seconds on the same thousand number of records. Once I have the data I don’t need to do much more than display it. Is there a more efficient way of doing this? I am aware that I can manually make SQL queries from models and controllers, I don’t know if this would be a viable option as I have not found a clear way to work with the result once the query is made.
Probably what it’s happening is that Rails is instantiating an object per row requested or used on the sum, or keeping in memory information to help that calculation.
Try using the find_by_sql method, something like:
And see how much it takes.
Also, check your logs. They gave you plenty of information on where is taking so long render.