每天,上周和上个月计算访客点击次数 [英] Count visitor hits per day, last week and last month

查看:114
本文介绍了每天,上周和上个月计算访客点击次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何使用文章制作一个网站,并希望列出每篇文章的观点,以显示最受欢迎的文章:今天,本周和本月。你做数据库模式吗?

解决方案

如果足够知道文章显示的次数,可以使用如下所示:

  daily_article_views(
,article_id
,view_date
,view_count
,主键(article_id,view_date)
,外键(article_id)引用文章
);

对于每篇文章视图,您将使用当前日期和当前日期的ID查找表中的一行目前正在查看的文章。如果存在一行,则增加计数器。如果没有行存在,则为view_count(和截断的日期)插入值为1的值。
然后,您可以使用SUM(view_count)将数据从该表汇总到每周,每月或每年的数字。



但是,如果您需要存储有关每个个人视图的信息,您可以使用类似于以下内容的表格结构。

  article_views(
,view_id
,article_id
,view_date
,additional_data_about_each_view
,主键(view_id)
,外键(article_id)引用文章
);

additional_data_about_each_view将是表示某些用户ID,也可能是IP地址或需要存储的任何列的列。请注意,在本设计中,您将存储文章视图的时间,包括时间(小时,分钟,秒)。这将使您能够在一天的时间内分析数据,正是在查看文章时。



最后一个设计的缺点是它可能会生成一个很多可能会使查询速度较慢的数据。当然,没有什么阻止你实施两种方案:)


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. 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.

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. 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 :)

这篇关于每天,上周和上个月计算访客点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆