实施"观看次数最多"的特征数据库

Stf*_*Pad 6 mysql sql database youtube

我想知道如何在我的数据库中最好地实现" 观看最多 "的功能(如youtube).

Let me explain the "most viewed" feature a little bit better: Basically I want to list the most visited pages/video/etc from this day/week/month, see http://www.youtube.com/charts/videos_views for an example.

So I was wondering how to best implement this feature as I can think of many many ways of doing it but all of them have their + and - to them.

Plus I also would love to hear the comments of various programmers on others programmers ideas. Also i would like to start a good conversation on this topic.

Ps. I'm looking specially on how to calculate the time, say most viewed in this month, without having a huge table saving every single view with the datetime. Any idea is welcome.
Pps. I use Mysql and PHP, extra tips for those two are very welcome.

ova*_*riq 3

有以下表格:1.视图2.views_hourly_summary 3.views_daily_summary 4.views_monthly_summary 5.views_alltime_summary

按以下时间间隔运行 cron 作业:

  1. 每小时运行一次,并从views表中预聚合该小时的视图,并将预聚合结果保存在views_hourly_summary表中,同时更新views_alltime_summary表

  2. 在每天结束时运行,并从小时表中预聚合当天的视图,并将预聚合结果保存在views_daily_summary 表中

  3. 在每个月末运行并从小时表中预聚合当天的视图,并将预聚合结果保存在views_daily_summary表中

接下来,在获取结果时,您必须进行一些数学计算,如下所示:

  1. 例如,您想要获取最近 4 小时的视图,您可以从每小时表中获取 3 个整小时的数据,并从视图表中获取剩余的数据,如下所示:

    选择 item_id, sum(views) 作为views_hourly_summary 中的视图,其中 concat(left(now() - 间隔 3 小时, 14), '00:00') 和 concat(left(now(), 14), '00: 之间的小时: 00') 按 item_id 分组

    联盟

    从日期时间介于 (now() - 间隔 4 小时) 和 concat(left(now() - 间隔 3 小时, 14), '00:00') 或 datetime > concat( 的视图中选择 item_id, count(1) 作为视图left(now(), 14), '00:00') 按 item_id 分组