Best way to maintain a customer's account balance

前端 未结 8 578
孤独总比滥情好
孤独总比滥情好 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 10:59

    That would be a function of how often you need to access this information. If its once in a while, then I'd say go ahead and recalculate it.

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

    For performance, I'd say both. Keep a log of all the transactions (in a separate table) but maintain a field in the customer record that stores the current balance that gets refreshed when you add more transactions.

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

    One project I worked on we stored the current balance in one field, and all the transactions on another table, but because of the level of importance that this project had on the data being perfect 100% (or better) or the time, we also stored a hash of the balance in another field and the hash was compared each time it was called to ensure integrity, it if did not match up it was recalculated from the transactions table, and re-hashed then sent to the customer support dept. for review.

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

    I think this depends on a lot of factors, how often will you be accessing the balance, how complex is it to recalculate it everytime you need it. What are the overheads of having views, etc.

    Purely on the face of the information you have given I would store the value as recalculating it from scratch each time could be a pain.

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

    Everyone here's right. It does depend. But you can have the best of both worlds by using a view. Sounds like you might have a fairly small system, and dynamically calculating the balance will be the easiest thing to do. In order to keep it simple, I would define a single view that has the account data you want (calculated dynamically).

    If you're going to need more performance than that, I would set up a trigger-based system for updating the balance to the main account table, then update the view behind the scenes so you don't need to change any other code. Just make sure you're using the right database isolation mode for either query, or you'll be in trouble! ;-)

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

    Use views and queries - you'd be suprised at how fast it'll run.

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