U
U
Urukhayy2018-05-15 13:04:44
Programming
Urukhayy, 2018-05-15 13:04:44

Caching the result vs constant recalculation of the same?

The server sends some data to the clients. For example, a report on store statistics per day. It contains rows with totals of purchases including all taxes etc. and the last row is the sum of all categories (it is required to sum many things and many rows). The total tax for all goods, the total amount of purchases, etc. That is, with each request to the server, you need to give this data to everyone who asks. What is the best way to organize this - each time recalculate the amount for a new one for each request, or store the "amount" field in the database and increase it when adding a new purchase?
PS If you constantly consider the same thing, theoretically, the process will work in vain. Why endlessly recalculate the same thing when you can just store the result and update it as new rows arrive in the table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vaut, 2018-05-15
@vaut

How long does this summation take?
How many times a day do you need to give this statistics?
How long does it take to deliver the rest of the content?
Early optimization will not lead to good.
You can spend a lot of time and nerves validating the cache and not see any difference in performance.

F
FreeBa, 2018-05-15
@FreeBa

One of the greats said. "In programming, there are only two really difficult things - it's inventing names for variables and invalidating the cache." (c)
If the power allows you to do without caching, then it's better not to use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question