MySQL timed task event, storage process (periodically delete data from the specified table 90 days before the specified time)

Source: Internet
Author: User

 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
    1. <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
  1. CREATE TABLE ' Tbl_base_count ' (
  2. ' ID ' int (one) not NULL auto_increment,
  3. ' CAPTCHA ' varchar (COLLATE) utf8_bin not NULL,
  4. ' PHONE ' varchar (COLLATE) utf8_bin not NULL,
  5. ' Sendtime ' varchar (+) COLLATE utf8_bin not NULL,
  6. PRIMARY KEY (' id ')
  7. ) Engine=innodb auto_increment=15 DEFAULT Charset=utf8 collate=utf8_bin;

2. Create Event E_del_tbl_base_count: [SQL]View Plaincopy
    1. CREATE EVENT ' E_del_tbl_base_count '
    2. On SCHEDULE every 1 day starts ' 2013-06-23 17:33:43 '
    3. 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
  1. P_del_count
  2. DELIMITER $$
  3. --
  4. --Stored procedures
  5. --
  6. CREATE PROCEDURE ' P_del_count ' (in' Date_inter ' INT)
  7. BEGIN
  8. 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;
  9. end$$
  10. 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
    1. SHOW VARIABLES like ' Event_scheduler ';
    2. SELECT @ @event_scheduler;
    3. SHOW processlist;
5. (ii) There are 4 ways to turn on the event Plan (scheduler) switch: [SQL]View Plaincopy
    1. SET GLOBAL event_scheduler = 1;
    2. SET @@ Global.event_scheduler= 1;
    3. SET GLOBAL event_scheduler = on ;
    4. 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
    1. Open an event:ALTER event e_del_logs on completion PRESERVE ENABLE;
    2. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.