使用mysql存储过程将秒级日志合并为分钟级日志

现有一个视频系统,每秒记录一条用户的观看情况,后来用户多了发现实在太影响行性能了,打算每分钟记录一次。

思路:循环将计算每个用户的观看日志,并整除60,得到的数值即为要保留的日志数量。

drop procedure if exists tomin;
delimiter $$
create procedure tomin()
begin
  declare s int default 0;
  declare stuId int;
  declare logCount int;
  DECLARE students CURSOR FOR select id from wk_student_info where loginName is not NULL;
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;
  OPEN students;
  fetch students into stuId;
  while s <> 1 do
        select (1 + count(*) - count(*) div 60) into logCount from wk_study_info where studentId = stuId;
        select stuId,logCount;
        delete from wk_study_info where studentId = stuId limit logCount;
        fetch students into stuId;
  end while;
  close students;
end $$
delimiter ;
call tomin();

2 条评论

[/0o0] [..^v^..] [0_0] [T.T] [=3-❤] [❤.❤] [^v^] [-.0] [!- -] [=x=] [→_→] [><] 更多 »
昵称
  1. Wally Google Chrome 65 Google Chrome 65 GNU/Linux GNU/Linux

    最后发现,还是你的博客文章页舒服。。。。 [><]

    1. 鸽子 鸽子 Google Chrome 67 Google Chrome 67 Windows 10 Windows 10

      你不是说丑么hhh