MySQL timed task event, storage process (periodically delete data from the specified table 90 days before the specified time)Category: mysql5.x2014-06-23 15:16 1266 People read comments (0) favorite reports MySQL Database
[SQL]View Plaincopy
- <span style="font-family: ' Microsoft Yahei '; font-size:14px; " >mysql timed Task event</span>
Due to some business requirements, we may need to periodically purge the database of some obsolete data, which can be done using MySQL's stored procedures and events. The following example periodically clears the data in log table Tbl_base_count before the specified number of days 1. Create a log table Tbl_base_count:
[SQL]View Plaincopy
- CREATE TABLE ' Tbl_base_count ' (
- ' ID ' int (one) not NULL auto_increment,
- ' CAPTCHA ' varchar (COLLATE) utf8_bin not NULL,
- ' PHONE ' varchar (COLLATE) utf8_bin not NULL,
- ' Sendtime ' varchar (+) COLLATE utf8_bin not NULL,
- PRIMARY KEY (' id ')
- ) Engine=innodb auto_increment=15 DEFAULT Charset=utf8 collate=utf8_bin;
2. Create Event E_del_tbl_base_count:
[SQL]View Plaincopy
- CREATE EVENT ' E_del_tbl_base_count '
- On SCHEDULE every 1 day starts ' 2013-06-23 17:33:43 '
- On completion not PRESERVE ENABLE does call P_del_count (90);
The above code indicates that the p_del_count stored procedure is executed once every day from 17:33:43 2013-07-30 and takes the parameter 3. Create a stored procedure:
[SQL]View Plaincopy
- P_del_count
- DELIMITER $$
- --
- --Stored procedures
- --
- CREATE PROCEDURE ' P_del_count ' (in' Date_inter ' INT)
- BEGIN
- DELETE from LOG WHERE (To_days (now ())-To_days (From_unixtime (<span style="Font-family:consolas, ' Courier New ', Courier, mono, serif; line-height:18px; " >SENDTIME</span>)) >=date_inter;
- end$$
- DELIMITER;
By the event of the parameter 90, delete the operation time 90 days before the data so that MySQL will be customized to perform this task every day. 4. (a) to see if the event plan is currently turned on (scheduler) there are 3 ways:
[SQL]View Plaincopy
- SHOW VARIABLES like ' Event_scheduler ';
- SELECT @ @event_scheduler;
- SHOW processlist;
5. (ii) There are 4 ways to turn on the event Plan (scheduler) switch:
[SQL]View Plaincopy
- SET GLOBAL event_scheduler = 1;
- SET @@ Global.event_scheduler= 1;
- SET GLOBAL event_scheduler = on ;
- SET @@ Global.event_scheduler= on ;
The key value 1 or on means open; 0 or off means close;
6. (iii) Event opening and closing:
[SQL]View Plaincopy
- Open an event:ALTER event e_del_logs on completion PRESERVE ENABLE;
- Close an event:ALTER event e_del_logs on completion PRESERVE DISABLE;
MySQL timed task event, storage process (periodically delete data from the specified table 90 days before the specified time)