Hi, I have a site with songs. The weekly/monthly/ and total stats are tracked for plays, score and downloads. My current method is terrible. To make a long story short; it tracks each play by putting a row into the monthly_plays table with songid and date. A nightly CRON deletes all entries > 30 day and then tallies the rest to find the total plays for each songid. It then runs through again and tallies < 8 day for weekly total. A server nightmare.
I'm thinking of one table with the (unique) songid and fields for each stat
stats_daily
songid - PRI KEY
plays
downloads
score
When a song is played it will "update plays=plays+1 where songid=songid" instead of adding a row. Every night a CRON loops through the table, deleted entries < 8 day, and inserts all rows from the daily_table into the weekly_table along with the date.
stats_weekly
songid
plays
downloads
score
date
So now each songid should have 7 entries in the weekly table and I can just use the SUM of "plays" column for the weekly plays.
Here are some concerns already:
Should I truncate the Daily Table or just set all stat fields to 0? It would be nice to have each songid already existing so I wouldn't have to check to see if it exists before updating it. I considered REPLACE INTO but that wouldn't increment the plays value. Maybe do a speedy truncate and then loop through the songs table to populate with songids again?
Can anybody think of a method that would allow me to store the weekly_plays total as one row instead of the 7 rows I'm considering? I've thought about having a table strictly for plays with 15 rows.
weekly_plays
songid
plays1
playsdate1
plays2
playsdate2
...
plays7date
It would then update the oldest playsdate and matching plays column; therefore only having one row per songid and the benefit to keep songid as a unique field. I don't know how I could tell php to "find the oldest playdate column, (playdate6) and then update playdate6 and plays6"?
I would also need to duplicate whatever method I use for the monthly_plays so then we'd be talking about 60 plus columns so that may not be the way to go either.
Any suggestions? I've been struggling with this one for a year.