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