I have an application where users's activity will be rewarded with points. There will be a points chart that will assign a certain amount of points for certain action.
My question is which approach is better and most importantly why?
Approach 1: - create a userPoints table in mysql and insert points there at each user action. When the user goes to their profile, query the database for the number of points and display them.
example:
user buys an item
query the database: insert 5 points in userPoints for user 2
user follows a place
query the database: insert 7 points in userPoints for user 2
user sells their item
query the database insert -5points in userPoints for user 2
Approach 2: - do not store the points in a table, but count the number of actions user has completed (get their count and type from the db) and then multiply by the certain amount of points for each type of action all at runtime, whenever number of points is required (for example when they visit their profile)
example:
user visits their profile to see how many points they have
query the database and count items possessed, places followed, friendships and then multiply items count x5, followings count x7 and friendships count x10 and display the number
EDIT for Steven:
let me get this straight, is this the chronology of actions I am supposed to take? How can I tighten this up and reduce the number of queries?
- buying an item has an actionId of 1 and points rewarded are 5 (this is specified in the - actions table in the db)
- Kylie (user ID: 1) buys an item.
- Query DB: Add the item to userItems table for user ID1
- Query DB: Register an action in the userActions table - userId: 1, actionId: 1
- Query DB SET userPointsTotal = userPointsTotal + points rewarded (nest two queries in one like this: set userPointsTotal = userPointsTotal + (select points from actions where actuionid= 1))
- Query DB get userPointsTotal and display value at Kylie's profile
- In the future, refer table userActions so user can get status title, depending on certain type of activity (social status or possessions status)