Accepted answer

if the amount of data is going to be really huge and you can sacrifice some realtime-ish manner of presenting it, the best way would be to compute the data for charts in some seperate database table and show the charts out of it. you can setup a background process (using whenever or delayed_job or whatever you like) to periodically update the pre-processed data table with fresh values.

another option would be caching the chart response by any means you like (using built-in rails caching, writing your custom cache etc) to deliver the same data to a big number of users with reduced db hit.

however, in general, preprocessing seems to be the winner as stats tables usually contain very "sparse" data which can be pre-computed to a much smaller set to display on a chart yet having the option to apply some filtering / sorting if needed.

edit just forgot to mention there might be some room for optimization on the database side. for instance, if you can limit the periods the user is able to view data for (= cap the amount to be queried per used), than proper indexing and db setup can deliver substantial performance without man-in-the middle things like caching or precomputing.

Related Query

More Query from same tag