I need to store points for users. Each point is to be stored in a separate row with a date. Now, when a user opens his page, it will display the total points for him.
Now to calculate the total points, I can think of the following options:
-
Every time the user opens his page, calculate the sum of all his points in that ONE mysql table and display it to him
-
Create another table for the total points, and whenever a point is added, calculate the sum, and replace the previous sum value with the new one. When user opens his page, it only accesses that ONE row in that total sum table and displays the value to him
-
Create a CRON job to run everynight to calculate the user’s total point and store it in the separate table created in (2)
To me, option no 2 looks efficient, but just want to know how something like that is normally done?
If you have the proper permissions you can create a trigger on the points table.
This will cause the total points column to update each time a row is inserted into your points table.