Basically I have 2 tables. users and users_activity . I use a mysql table and the number of users is almost 5000. Each user has many actions, about 50-150 operations. When I want to get the user list table (ten by ten), I need to display the date of the last user activity. In this case, I have two options:
First option:
I add the last_activity column to users and select like this:
SELECT * FROM users ORDER BY id DESC, lIMIT 0, 10
If I want to add a new action:
INSERT INTO users_activity (userId, date) VALUES(19, "2016-04-06") UPDATE users SET last_activity = "2016-04-06" WHERE id = 19 LIMIT 1
If I want to cancel last_activity :
DELETE FROM users_activity WHERE activity_id = 100 LIMIT 1 SELECT date FROM users_activity WHERE userId = 19 ORDER BY DESC LIMIT 1
Using this selection, I can get the latest users_activity FROM users_activity and use it in sql update.
UPDATE users SET last_activity = "2016-04-02" WHERE id = 19 LIMIT 1
The second option:
I last_activity column from users and select like this:
SELECT users.*, users_activity.date FROM users LEFT JOIN users_activity ON users_activity.userId = users.id GROUP BY users.id ORDER BY users.id DESC, users_activity.date DESC LIMIT 0, 10
If I want to add a new action:
INSERT INTO users_activity (userId, date) VALUES(19, "2016-04-06")
if I want to cancel last_activity :
DELETE FROM users_activity WHERE activity_id = 100 LIMIT 1
I am using mysql. Both tables: innoDB .
In this situation, in which direction would you recommend me and why?