5.计数器表
创建一张独立的表存储计数器通常是个好主意,小且快
假如有一个计数器表,只有一行数据,记录网站的点击次数:
create table hit_counter(
cnt int unsigned not null
);
每次网站点击都会对计数器进行更新:
update hit_counter set cnt = cnt + 1;
如果并发量比较高,又很多事务想要更新这一行,就会有一个全局的互斥锁,让这些事务只能串行执行,可以将计数器保存在多行中,每次随机选择一行进行更新。
create table hit_counter1(
slot tinyint unsigned not null primary key,
cnt int unsigned not null
);
然后预先增加100行数据,现在选择一个随机的槽(slot)进行更新:
update hit_counter set cnt = cnt + 1 where slot = rand() * 100;
执行过程会报错 error 1175 , 可以通过设置sql_safe_updates为0,关闭限制
要获得统计结果,需要使用下面的聚合查询:
select sum(cnt) from hit_counter;
还有一个常见的需求是每隔一段时间开始一个新的计数器(例如,每天一个)。
create table daily_hit_counter(
day date not null,
slot tinyint unsigned not null,
cnt int unsigned not null,
primary key(day, slot)
);
在这种情况下不用预先生成行,而用on duplicate key update 代替:
insert into daily_hit_counter(day, slot, cnt) values (current_date, rand() * 100, 1) on duplicate key update cnt = cnt + 1;
如果需要减少表的行数,可以写一个周期执行的任务,合并所有结果到0号槽,并且删除所有其他的槽:
update daily_hit_counter as c
inner join(
select day, sum(cnt) as cnt, min(slot) as mslot
from daily_hit_counter
group by day
) as x on c.day = x.day
set c.cnt = if(c.slot = x.mslot, x.cnt, 0),
c.slot = if(c.slot = x.mslot, 0, c.slot);
delete from daily_hit_counter where slot <> 0 and cnt = 0;