简体   繁体   中英

Is creating a table for the last 30 days efficient or is there a better way?

Basically I have a site where users can upload and share files. I would like it so users can keep track how many downloads were made in the last 30 days and be able to view each of those 30 days individually.

I was thinking of creating a table that'll have 31 columns, 1 column being the id and the rest being day1, day2, day3,etc... Updating this table every day moving the downloads of 1 day to the day before.

My question is would this be efficient or is there a better way of going about this?

You would be better off having a download table which links to your file table. The download table would create a record each time a file is downloaded.

id
file_id
ip_address
user_agent
created

etc.

Then write a query to determine how many downloads happened a day would be quite an easy task.

In order to accurately determine the number of elapsed days that meet your criteria, I would suggest that you determine the difference between the current date and the date the record was submitted. Utilizing this method would then allow you to determine what should be included and what shouldn't, without the need of a complicated column structure.

This wouldn't be particularly efficient. I expect a better method would be to have a table:

UniqueID
FileID
DownloadTimeStamp

And record every download in a row. With appropriate indexing (FileID, DownloadTimeStamp), so can do analytics on the fly.

Depending upon what you mean by each day (in some global time like UTC, or in the user's timezone), you could roll these up to:

FileID
Day
Count

To reduce the work of of the analysis if it seems to be a performance problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM