MYSQL event更新数据实例

MYSQL event更新数据实例


1. 创建历史备份表

CREATE TABLE `exam_his` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `total_reading_num` bigint(20) DEFAULT NULL,
  `mandarin_serve_num` bigint(20) DEFAULT NULL,
  `english_serve_num` bigint(20) DEFAULT NULL,
  `mhk_serve_num` bigint(20) DEFAULT NULL,
  `exam_total` bigint(20) DEFAULT NULL,
  `total_serve_school` bigint(20) DEFAULT NULL,
  `total_test_num` bigint(20) DEFAULT NULL,
  `total_reading_num_week` bigint(20) DEFAULT NULL,
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `manage_his` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `province_construct_num` bigint(20) DEFAULT NULL,
  `city_construct_num` bigint(20) DEFAULT NULL,
  `country_construct_num` bigint(20) DEFAULT NULL,
  `total_class` bigint(20) DEFAULT NULL,
  `total_attendance` bigint(20) DEFAULT NULL,
  `total_interactive` bigint(20) DEFAULT NULL,
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `resource_his` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `resource_num` bigint(20) DEFAULT NULL,
  `increase_num` bigint(20) DEFAULT NULL,
  `teaching_design_num` bigint(20) DEFAULT NULL,
  `teaching_file_num` bigint(20) DEFAULT NULL,
  `media_material_num` bigint(20) DEFAULT NULL,
  `test_evaluate_num` bigint(20) DEFAULT NULL,
  `exercise_num` bigint(20) DEFAULT NULL,
  `exercise_packge_num` bigint(20) DEFAULT NULL,
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `teaching_his` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `teacher_serve_num` bigint(20) DEFAULT NULL,
  `student_serve_num` bigint(20) DEFAULT NULL,
  `teacher_use_num` bigint(20) DEFAULT NULL,
  `student_use_num` bigint(20) DEFAULT NULL,
  `teaching_num` bigint(20) DEFAULT NULL,
  `interactive_num` bigint(20) DEFAULT NULL',
  `homework_num` bigint(20) DEFAULT NULL,
  `exam_answer_num` bigint(20) DEFAULT NULL,
  `intel_subject_num` bigint(20) DEFAULT NULL,
  `intel_testpaper_num` bigint(20) DEFAULT NULL,
  `report_num` bigint(20) DEFAULT NULL,
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. 创建存储过程

  • updateResourceData(每周日24点更新)
CREATE PROCEDURE `updateResourceData`()
BEGIN
  set @inc_resource_num_min = 24300;
  set @inc_resource_num_max = 24359;
  set @inc_increase_num_min = 2;
  set @inc_increase_num_max = 3;
  set @inc_teaching_design_num_min = 120;
  set @inc_teaching_design_num_max = 126;
  set @inc_teaching_file_num_min = 120;
  set @inc_teaching_file_num_max = 130;
  set @inc_media_material_num_min = 720;
  set @inc_media_material_num_max = 731;
  set @inc_test_evaluate_num_min = 180;
  set @inc_test_evaluate_num_max = 185;
  set @inc_exercise_num_min = 80;
  set @inc_exercise_num_max = 87;
  set @inc_exercise_packge_num_min = 22900;
  set @inc_exercise_packge_num_max = 22982;
  
	INSERT INTO resource_his(resource_num, increase_num, teaching_design_num, teaching_file_num, media_material_num,
	                         test_evaluate_num, exercise_num, exercise_packge_num) 
	SELECT resource_num, increase_num, teaching_design_num, teaching_file_num, media_material_num,
	                         test_evaluate_num, exercise_num, exercise_packge_num FROM resource WHERE id = 1;
  UPDATE resource SET
    resource_num = resource_num + FLOOR(@inc_resource_num_min + (RAND() * (@inc_resource_num_max - @inc_resource_num_min + 1))),
	increase_num = increase_num + FLOOR(@inc_increase_num_min + (RAND() * (@inc_increase_num_max - @inc_increase_num_min + 1))),
	teaching_design_num = teaching_design_num + FLOOR(@inc_teaching_design_num_min + (RAND() * (@inc_teaching_design_num_max - @inc_teaching_design_num_min + 1))),
	teaching_file_num = teaching_file_num + FLOOR(@inc_teaching_file_num_min + (RAND() * (@inc_teaching_file_num_max - @inc_teaching_file_num_min + 1))),
	media_material_num = media_material_num + FLOOR(@inc_media_material_num_min + (RAND() * (@inc_media_material_num_max - @inc_media_material_num_min + 1))),
	test_evaluate_num = test_evaluate_num + FLOOR(@inc_test_evaluate_num_min + (RAND() * (@inc_test_evaluate_num_max - @inc_test_evaluate_num_min + 1))),
	exercise_num = exercise_num + FLOOR(@inc_exercise_num_min + (RAND() * (@inc_exercise_num_max - @inc_exercise_num_min + 1))),
	exercise_packge_num = exercise_packge_num + FLOOR(@inc_exercise_packge_num_min + (RAND() * (@inc_exercise_packge_num_max - @inc_exercise_packge_num_min + 1))),
	update_time = CURRENT_TIMESTAMP
  WHERE 1 = 1;
  
  COMMIT;
END;
  • updateScreenData(每周六、日,10、16点更新)
CREATE PROCEDURE `updateScreenData`()
BEGIN
  set @inc_exam_total_min = 900;
  set @inc_exam_total_max = 919;
  set @inc_total_serve_school_min = 4;
  set @inc_total_serve_school_max = 6;
  set @inc_total_test_num_min = 1800;
  set @inc_total_test_num_max = 1840;
  set @inc_teacher_serve_num_min = 30;
  set @inc_teacher_serve_num_max = 39;
  set @inc_student_serve_num_min = 300;
  set @inc_student_serve_num_max = 344;
  set @inc_teacher_use_num_min = 80000;
  set @inc_teacher_use_num_max = 80524;
  set @inc_student_use_num_min = 654000;
  set @inc_student_use_num_max = 654703;
  set @inc_teaching_num_min = 3200;
  set @inc_teaching_num_max = 3248;
  set @inc_interactive_num_min = 6000;
  set @inc_interactive_num_max = 6152;
  set @inc_homework_num_min = 41000;
  set @inc_homework_num_max = 41302;
  set @inc_exam_answer_num_min = 3100;
  set @inc_exam_answer_num_max = 3180;
  set @inc_total_attendance_min = 102000;
  set @inc_total_attendance_max = 102181;
  set @inc_total_interactive_min = 9100;
  set @inc_total_interactive_max = 9168;
  
	INSERT INTO exam_his(total_reading_num, mandarin_serve_num, english_serve_num, mhk_serve_num,
	                     exam_total, total_serve_school, total_test_num, total_reading_num_week) 
	SELECT total_reading_num, mandarin_serve_num, english_serve_num, mhk_serve_num, 
	                     exam_total, total_serve_school, total_test_num, total_reading_num_week FROM exam WHERE id = 1;
  -- 更新exam表
  UPDATE exam SET 
      exam_total = exam_total + FLOOR(@inc_exam_total_min + (RAND() * (@inc_exam_total_max - @inc_exam_total_min + 1))),
	  total_serve_school = total_serve_school + FLOOR(@inc_total_serve_school_min + (RAND() * (@inc_total_serve_school_max - @inc_total_serve_school_min + 1))),
	  total_test_num = total_test_num + FLOOR(@inc_total_test_num_min + (RAND() * (@inc_total_test_num_max - @inc_total_test_num_min + 1))),
	  update_time = CURRENT_TIMESTAMP
  where id = 1;
	
	INSERT INTO teaching_his(teacher_serve_num, student_serve_num, teacher_use_num, student_use_num, teaching_num,
	                         interactive_num, homework_num, exam_answer_num, intel_subject_num, intel_testpaper_num, report_num) 
	SELECT teacher_serve_num, student_serve_num, teacher_use_num, student_use_num, teaching_num,
	                         interactive_num, homework_num, exam_answer_num, intel_subject_num, intel_testpaper_num, report_num FROM teaching WHERE id = 1;
  -- 更新teaching表
  UPDATE teaching SET
	  teacher_serve_num = teacher_serve_num + FLOOR(@inc_teacher_serve_num_min + (RAND() * (@inc_teacher_serve_num_max - @inc_teacher_serve_num_min + 1))),
	  student_serve_num = student_serve_num + FLOOR(@inc_student_serve_num_min + (RAND() * (@inc_student_serve_num_max - @inc_student_serve_num_min + 1))),
	  teacher_use_num =  teacher_use_num + FLOOR(@inc_teacher_use_num_min + (RAND() * (@inc_teacher_use_num_max - @inc_teacher_use_num_min + 1))),
	  student_use_num =  student_use_num + FLOOR(@inc_student_use_num_min + (RAND() * (@inc_student_use_num_max - @inc_student_use_num_min + 1))),
	  teaching_num =  teaching_num + FLOOR(@inc_teaching_num_min + (RAND() * (@inc_teaching_num_max - @inc_teaching_num_min + 1))),
	  interactive_num = interactive_num + FLOOR(@inc_interactive_num_min + (RAND() * (@inc_interactive_num_max - @inc_interactive_num_min + 1))),
	  homework_num = homework_num + FLOOR(@inc_homework_num_min + (RAND() * (@inc_homework_num_max - @inc_homework_num_min + 1))),
	  exam_answer_num =  exam_answer_num + FLOOR(@inc_exam_answer_num_min + (RAND() * (@inc_exam_answer_num_max - @inc_exam_answer_num_min + 1))),
	  update_time = CURRENT_TIMESTAMP
  where id = 1;
  
	INSERT INTO manage_his(province_construct_num, city_construct_num, country_construct_num,
	                       total_class, total_attendance, total_interactive) 
	SELECT province_construct_num, city_construct_num, country_construct_num,
	                       total_class, total_attendance, total_interactive FROM manage WHERE id = 1;
  -- 更新manage表
  UPDATE manage SET
	  total_attendance = total_attendance + FLOOR(@inc_total_attendance_min + (RAND() * (@inc_total_attendance_max - @inc_total_attendance_min + 1))),
	  total_interactive =  total_interactive + FLOOR(@inc_total_interactive_min + (RAND() * (@inc_total_interactive_max - @inc_total_interactive_min + 1))),
	  update_time = CURRENT_TIMESTAMP
  where id = 1;
 
  commit;
END;

3. 查看定时任务开关是否开启

show VARIABLES like ‘%event_scheduler%’;

4. 开启定时任务

set GLOBAL event_scheduler=1;

注意:此处开启方式会在mysql数据库重启后失效,如果权限允许,建议修改mysql配置文件

5. 创建定时事件

CREATE EVENT `updateResourceDataOnSundayEve` 
ON SCHEDULE EVERY 1 WEEK 
STARTS '2019-04-08 00:00:00' 
ON COMPLETION NOT PRESERVE ENABLE 
DO 
call `updateResourceData`;
CREATE EVENT `updateScreenDataOnSaturdayMoring` 
ON SCHEDULE EVERY 1 WEEK 
STARTS '2019-04-06 10:00:00' 
ON COMPLETION NOT PRESERVE ENABLE 
DO 
call `updateScreenData`;
CREATE EVENT `updateScreenDataOnSaturdayAfternoon` 
ON SCHEDULE EVERY 1 WEEK 
STARTS '2019-04-06 16:00:00' 
ON COMPLETION NOT PRESERVE ENABLE 
DO 
call `updateScreenData`;
CREATE EVENT `updateScreenDataOnSundayMorning` 
ON SCHEDULE EVERY 1 WEEK 
STARTS '2019-04-07 10:00:00' 
ON COMPLETION NOT PRESERVE ENABLE 
DO 
call `updateScreenData`;
CREATE EVENT `updateScreenDataOnSundayAfternoon` 
ON SCHEDULE EVERY 1 WEEK 
STARTS '2019-04-07 16:00:00' 
ON COMPLETION NOT PRESERVE ENABLE 
DO 
call `updateScreenData`;

6. 验证跟踪方案

  • 在对应的时间点分别查看业务表和历史备份表的数据变化,各表中的update_time字段将是可直观了解数据表信息的关键指标。
    在这里插入图片描述新插入的历史数据
  • SELECT * FROM information_schema.EVENTS; 语句可直接查看mysql event执行情况。
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值