5

I'm making a Rails app which will feature a lot of "calculated" or "aggregated" data, i.e. information which is computed by performing expensive operations on the data stored by the user. I'm thinking I need some way of storing this data so A. I am not constantly performing expensive DB operations, and B. So that I can spit out "reports" with pretty graphs over time etc for given attributes.

I'm wondering what the best way to implement this is? I'd need to be calculating and storing values (numeric) for a given model, and how they change over time. I want this to be efficient and avoid duplicating and data. The records will be pretty much fixed once created, so I don't need to worry about things being changed too much, though it needs to be considered.

I just wondered what the most common approach to this is, and how I should go about implementing this within a Rails app?

4

1 回答 1

1

我一直在研究类似的问题,并且我也研究过以错误方式执行此操作的应用程序。

这是我的最佳实践建议:

  • 将原始数据存储在模型中,我们称之为Feed
  • 建立与另一个保存计算值的模型的一对一关联,例如FeedStats. 这也可能是一对多关联或多对一,具体取决于具体情况;您可能会将一些单独Feed的记录汇总到某种聚合中,等等。
  • 保留所有原始原始数据。如果您稍后想要将计算切换到其他算法并且您可能需要重新计算旧数据,或者如果您发现计算中的错误等,这将非常有用。
  • 使用 Resque(带或不带调度程序)、DelayedJob 或类似工具等工具在后台任务上设置计算。

如果您可以更具体一些并给出一些确切问题的示例,我也许可以提供一些更具体的提示。祝你好运。

于 2012-07-15T21:31:22.263 回答