samedi 18 mai 2019

Do I need to calculate the field or update on each transaction?

Trying to design database. I have User table, which supposed to have balance field. It can be calculated each time I need balance based on 4 tables like below:

enter image description here

So I need to do some math like sum of all deposits, minus sum of withdrawals, minus bet amount+profit, plus amount from bonuses. There can be thousands of records for each user in each related table.

Or alternatively I can just update balance field from the application code whenever one of the related table has been altered.

However, first method is tend to be slower and slower as database grows. Second method is prone to errors, in case my application will fail to update the field and real balance will get asynced with balance field.

I wonder if there is any design pattern or technique to handle this cases? How the online bankings or similar services are counting balance? Are they going through each bank transaction every time balance is requested?

Aucun commentaire:

Enregistrer un commentaire