Answer the question
In order to leave comments, you need to log in
Input data is stored in MySQL, How to store and calculate data on the fly when one of the tables is updated?
The question is:
1) The input data is stored in MySQL, in the "Results" table:
Results [result_id, post_id , user_id , points_bet, points_got]
Posts [ post_id , category_id , text]
Categories [ category_id , category_name]
Users[ user_id , overall_ratio ]
The overall_ratio value for each user is calculated as follows
. way:
overall_ratio = SELECT sum(points_got) / sum(points_bet) FROM Results WHERE (user_id = user->id)
category_id_ratio = SELECT sum(points_got) / sum(points_bet) FROM Results WHERE (user_id = user->id) AND (category_id = category->id)
Answer the question
In order to leave comments, you need to log in
0)
The main question is how many values do you have in Query?
1)
And what for to collect results on all table?
When you add a record, you know the user_id, you know the category, respectively, you can recalculate only a specific line in its entirety
2)
Based on your formula - there is no need to honestly recalculate the line, you can do something like
This can be done both automatically by a trigger and in the logic of the application itself.
In principle, if the Query is large, you can move it to some kind of redis.
Re-read the question. You do not want to store exactly non-aggregated data, i.e. get rid of Results ?
If there the account does not go to hundreds of millions of records, I would still store it, because if anything, it would not be possible to recalculate the values otherwise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question