Best way to maintain a customer's account balance

前端 未结 8 616
孤独总比滥情好
孤独总比滥情好 2021-02-06 10:24

Is it better to have a field in the database that stores the customers account balance or use views and queries to generate the information.

相关标签:
8条回答
  • 2021-02-06 11:19

    "It depends". Most often, you want to avoid derived data. However, there are cases where having the derived total is justified.

    Case in point:

    I worked on a credit database application, where the balance was comprised of many different things, and different business rules over time. For example, the "balance" was actually a sum of different balances from different buckets, such as Principal, Fees, etc.

    As transactions were posted, they were allocated to different buckets according to business rules. Fees went to the fees bucket. Purchases, credits, and debits went to the principal bucket. These allocations and rules were subject to change over time.

    Imagine querying 100,000 customer balances on the fly in the face of changing business rules over time. This is a solid case where having the derived value actually make sense. We maintained a set of algorithms to "rewind" the account and reconstruct the balance chronologically for audit and verification purposes, but it was nothing you would want to do for large sets.

    0 讨论(0)
  • 2021-02-06 11:20

    If views and queries give you fast enough result then do not store in a separate field.

    If it's not fast enough then store it into a separate field. Since this field will store redundant information it is very important to keep it synchronized.

    0 讨论(0)
提交回复
热议问题