In a stock control system, which records the following entities in the database (this is over simplified of course):
- Items (for which the stock needs to be controlled)
- Stock movements:
- Input movements (they add up to certain item stock)
- Output movements (they substract from certain item stock)
Which is the best strategy and why:
- Store the current quantity for each item and update it every time a movement is recorded.
- Don't store the current quantity in each item and calculate it every time is needed as
item.current_qty = sum(item.input_movements.qty) - sum(item.output_movements.qty
(this is no particular language, it's just pseudo code)
Take into account that, being a stock control system, the current quantity for each item is used a lot.
Note: this question is similar to this other, but this one is especially about data that can be aggregated.