统计每天、上周和上个月的访客点击量
我制作了一个包含文章的网站,我想计算每篇文章的浏览量以显示最受欢迎的文章:今天、本周和本月。
您将如何为此制定数据库架构?
I make a site with articles and I want to count the views of each article for displaying most popular articles: today, this week and this month.
How would you do the database schema for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果知道一篇文章的显示次数就足够了,您可以使用如下内容:
对于每个文章视图,您将使用当前日期和当前正在查看的文章的 ID 在表中查找一行。如果存在一行,则增加计数器。如果不存在任何行,则插入该行时,view_count 的值为 1(以及截断的日期)。
然后,您可以根据需要使用 SUM(view_count) 将该表中的数据汇总为每周、每月或每年的数据。
但是,如果您需要存储有关每个单独视图的信息,您可以使用类似于以下的表结构。
extra_data_about_each_view 是代表某个用户 ID 的列,可能是 IP 地址或您需要存储的任何内容。请注意,在此设计中,您将存储文章查看的时间,包括时间(小时、分钟、秒)。这将使您能够分析一天内的数据,即查看文章的时间。
最后一种设计的缺点是它可能会生成大量数据,这可能会使查询速度变慢。当然,没有什么可以阻止您实现这两种选择:)
If it is enough to know the number of times an article was displayed you could use something like this:
For each article view, you would lookup a row in the table using the Current Date and the ID of the article currently being viewed. If a row exists, you increment the counter. If no row exists, you insert it with a value of 1 for view_count (and a truncated date).
You can then rollup the data from this table using SUM(view_count) to weekly, monthly or yearly figures as neccesary.
However, if you need to store information about each individual view, you could use a table structure similar to the following.
The additional_data_about_each_view would be columns representing some user id, maybe IP address or whatever you need to store. Note that in this design you will store the time of article view including the time (hour, minutes, seconds). This would eable you to analyze the data over the course of a day, exactly when the articles are viewed.
The downside with the last design is that it will potentially generate a lot of data which may make queries slower. Of course, nothing stops you from implementing both alternatives :)
如果您绝对不会对一天中的时间信息感兴趣,那么您可以使用包含 3 列的表格,
但您可能不一定希望为每次点击增加
view_count
列。您可以在应用程序中缓存一些信息并批量更新。If you will definitely never be interested in time of day information then you could use a table with 3 columns
You might not necessarily want to increment the
view_count
column for every hit though. You could cache some information in your application and update in batches.