I’m creating a system where users get paid to click advertisements and I need to keep track of the amount of advertisements a user has clicked for the last 7 days. To do this I figured 2 ways:
-
For each advertisement clicked, there will be inserted an entry into the database table ‘ads_clicked’. Each day the entries that are older than 7 days will be cleared (cronjob script).
-
For each advertisement clicked, there will be inserted an entry into the database table ‘ads_clicked’. However this time each log will only remain in the database table for 1 day. Each end of the day the ads clicked will be calculated for each user and saved in a field ‘ck_1’ ( in the table ‘users’ ) which holds the ads clicked yesterday. There’ll also be ck_2 (= ads clicked 2 days ago), ck_3, etc., which will be moved each day ( ck_7 = ck_6, ck_6 = ck_5, etc. ).
Both ways are possible but the 2nd way requires UPDATE queries in the cronjob script that’s ran daily and the 1st way does not. However using the 2nd way it will be easier to display the ads clicked over the last 7 days for each user as it’s just stored inside fields (ck_1, ck_2,etc.) which are part of the user entry in the database, while using the 2nd way I’d need to calculate it ( counting the rows in ‘ads_clicked’ table for each day ) every time I want to display the ads clicked over the last 7 days.
Do notice that the website may get huge traffic and thus lots of entries made to the ‘ads_clicked’ table, so I need to be sure which way is most efficient.
Thanks in advance.
The only real way to know how each option is going to preform with your data on your hardware is to try it both ways and measure the results. (And if you do that, post an update here.)
I created a similar system a while back and went with something like option 1. It’s the simplest and most straightforward way to do it. If you eventually determine that MySQL inserts just can’t possibly cope with the level of data you’re trying to insert, it should be pretty easy to replace this with a cache or a message queue or some other fancy new thing.