2010-10-26 24 views
0

表:MySQL的存儲隨時間視圖數據,如何引用

VIDEOID(vidID,vidName,vidURL)等

觀看次數(vidID,視圖,時間戳)

這樣做的主要目標數據庫將計算最近的視圖和前一個視圖之間的斜率。

比方說,我每天自動將視圖數據存儲在數據庫中。

這意味着所有視頻的所有視圖數據都在一個名爲viewCount的大表中。視圖通過vidID鏈接到相關視頻。這是否會成爲可接受的解決方案?主要是,如何從當前數據庫設置中檢索計算每個視頻的斜率所需的兩個值?

回答

0

爲什麼不使用匯總表(「實體化視圖」),如以下按年和周存儲視圖。您可以根據需要更改粒度級別 - 月度,日常等...

drop table if exists video_counts; 
create table video_counts 
(
year_id smallint unsigned not null, 
week_id tinyint unsigned not null, 
video_id int unsigned not null, 
counter int unsigned not null default 0, 
primary key (year_id, week_id, video_id) -- note clustered index (innodb only) 
) 
engine=innodb; 

insert into video_counts (year_id, week_id, video_id, counter) values 
(2010,1,1, 90), (2010,2,1, 80), (2010,3,1, 70), 
(2010,1,2, 30), (2010,2,2, 50), (2010,3,2, 30); 

update video_counts set 
    counter = counter + 1 
where 
    year_id = year(now()) and week_id = week(now()) and video_id = 1;