Show calculated results on the fly vs. Storing pre-calculated results in a table
I have results that are calculated from multiple rows in many different tables. These results are then displayed in a profile. To show the most current results on request, should i store these results in a separate table and upda开发者_如何学Gote them on change? or should i go with calculating these results on the fly?
As usual with performance questions, the answer is "it depends".
I'd probably start by calculating the results on the fly and go with precomputing them when it starts to be a problem.
If you have precomputed/summarized copies of your main data, you'll have to set up an updating process to make sure your summaries are correct. This can be quite tricky and can add a lot of complexity to your application so I wouldn't do it unless I had to. You'll also want to have a set of sanity check tools to make sure your summaries are, in fact, correct. And a set of "kill it all and rebuild the generated summaries" tools will also come in handy.
If these calculations are a problem (and by that I mean that you have measured the performance and know that your the queries in question are a bottleneck and you have the numbers to prove it) then it might be worth the extra coding and maintenance effort.
精彩评论