简体   繁体   English

计算每天,上周和上个月的访客点击量

[英]Count visitor hits per day, last week and last month

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? 你会如何为此做数据库架构?

If it is enough to know the number of times an article was displayed you could use something like this: 如果足以知道文章显示的次数,您可以使用以下内容:

daily_article_views(
  ,article_id
  ,view_date
  ,view_count
  ,primary key(article_id, view_date)
  ,foreign key(article_id) references articles
);

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. 对于每个文章视图,您将使用当前日期和当前正在查看的文章的ID查找表中的行。 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). 如果不存在任何行,则为view_count(以及截断日期)插入值为1的行。 You can then rollup the data from this table using SUM(view_count) to weekly, monthly or yearly figures as neccesary. 然后,您可以使用SUM(view_count)将此表中的数据汇总为每周,每月或每年的数据。

However, if you need to store information about each individual view, you could use a table structure similar to the following. 但是,如果需要存储有关每个视图的信息,可以使用类似于以下内容的表结构。

article_views(
  ,view_id
  ,article_id
  ,view_date
  ,additional_data_about_each_view
  ,primary key(view_id)
  ,foreign key(article_id) references articles
);

The additional_data_about_each_view would be columns representing some user id, maybe IP address or whatever you need to store. additional_data_about_each_view将是表示某些用户ID,可能是IP地址或您需要存储的任何内容的列。 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 :) 当然,没有什么能阻止你实现两种选择:)

If you will definitely never be interested in time of day information then you could use a table with 3 columns 如果您绝对不会对时间信息感兴趣,那么您可以使用包含3列的表格

article_id
date
view_count

You might not necessarily want to increment the view_count column for every hit though. 您可能不一定希望为每次命中增加view_count列。 You could cache some information in your application and update in batches. 您可以在应用程序中缓存一些信息并批量更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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