sql - Select sum or updating a field for Total Balance? -


i'm using entity framework , azure sql.

i have users , have records on balance table.some of users may have 1 million record.i need total balance of user before every http requests.

i have 2 approaches getting total balance of user:

first: insert balance , update totalbalance field (by itself) in transaction.

transaction( insertbalance(amount) update customersummary set totalbalance=totalbalance+amount ) 

if need total balance i'll select customersummary table.

second: inserts balance directly without using transaction. if need total balance have sum query.

is first approach reliable total balance ?

can sum on second approach fast first approach ?

the second approach guaranteed accurate -- if want sum of particular column, there nothing more accurate query calculates sum.

the reason maintaining summary table performance. typically, such table maintained in 1 of 2 ways:

  • triggers
  • stored procedures wrap all data modification operations

your example insert "application-side" solution. danger might come along , balance incorrect , have value changed directly in database. total doesn't changed.

to make work correctly, need have right controls on access database ensure whenever amount changes, dependencies change. note: not issue if calculate balance when need it.


Comments