Should I store a calculate value in my database along with the variables?

前端 未结 5 828
囚心锁ツ
囚心锁ツ 2020-12-31 07:00

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

5条回答
  •  Happy的楠姐
    2020-12-31 07:49

    It all depends on what resources are scarce in your environment. If you do pre-calculate the value, you'll save CPU time at the cost of increased network usage and DB storage space. These days, CPU time is generally much more abundant than network bandwidth and DB storage, so I'm going to guess that as long as the calculation isn't too complicated then pre-calculating the value is not worth it.

    On the other hand, perhaps the value you're calculating takes a substantial amount of CPU. In this case, you may want to cache that value in the DB.

    So, it depends on what you have and what you lack.

提交回复
热议问题