问题描述
在我使用数据库的所有应用程序中,我通常存储计算的值以及计算该值所需的变量。例如,如果我有 tonnage
和 cost
,我会乘以它们计算 / code>。我可以只是重新计算每次需要的值,我只是想知道是否有一个标准的方法。任何一种方式对我很好,我只想做最常见的。
In all the applications I have made where a database is used I typically store the calculated value along with the variables needed to calculate that value. For example, if I have
tonnage
and cost
I would multiply them to calculate the total
. I could just recalculate the value every time it is needed, I was just wondering if there was an standard approach. Either way is fine with me, I just want to do what is most common.
如果我存储计算变量,它使我的域类更复杂,我的控制器逻辑清理,如果我不存储计算的变量,它是另一种方式。
If I store the calculate variables it makes my domain classes a bit more complex, but makes my controller logic cleaner, if I don't store the calculated variables it is the other way around.
计算不会很频繁,但可能适度频繁,但是数学是便宜的?
The calculations would not be extremely frequent, but may be moderately frequent, but math is cheap right?
推荐答案
标准方法不是存储这种计算值 -
The standard approach is not to store this kind of calculated values - it breaks normalization.
有些情况下,您需要存储计算的值,如果它需要太长的时间重新计算,或者你正在运行一个数据仓库等。在你的情况下,你想坚持规范化规则。
There are cases you want to store calculated values, if it takes too long to recalculate, or you are running a data warehouse etc. In your case, you want stick to the normalization rules.
这篇关于我应该在我的数据库中存储一个计算值和变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!