1|0 Background Due to project requirements, each month's historical stock data needs to be archived and backed up, and some log tables need to have their detail fields cleared to save space on the MySQL database disk. Some scheduled tasks are needed to clean up this data regularly. 2|0 Technology Selection
3|0 Specific operations 3|1mysql configuration Since the scheduled task option is turned off in the default configuration of MySQL, for the sake of insurance, let's first check whether the option is turned on. show variables like '%event_scheduler%'; If the Off option is displayed, there are two ways to modify it: 1. Statement method (valid for the currently started instance, invalid after restart) set global event_scheduler=1; 2. Configure my.cnf (my.ini in Windows) and add the following options [mysqld] event_scheduler=ON Then save it and restart mysql 3|2sql writing Create a stored procedure (procedure) [Optional: If it is just a simple SQL, you can specify it directly in the scheduled task]
create PROCEDURE sched_clean() --name BEGIN -- Define parameters DECLARE begin_time BIGINT DEFAULT UNIX_TIMESTAMP(DATE_SUB(CURRENT_DATE(),interval 31 DAY))*1000; DECLARE end_time BIGINT DEFAULT UNIX_TIMESTAMP(DATE_SUB(CURRENT_DATE(),interval 30 DAY))*1000; -- Update data UPDATE test_table SET rule_cost=null WHERE start_time>begin_time and start_time<end_time; END Create a scheduled task Once the stored procedure is created, we can call it regularly. CREATE EVENT `demo`.`day_of_clean_event` ON SCHEDULE EVERY '1' DAY STARTS DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 HOUR) ON completion preserve disable DO call sched_clean(); Code Explanation:
Related queries Query all events of this machine SELECT event_name,event_definition,interval_value,interval_field,status FROM information_schema.EVENTS; Enable/disable an event that has already been created alter event event_name on completion preserve enable; //Open scheduled task alter event event_name on completion preserve disable; //Close scheduled task 4|0 Points worth noting If your global parameters are not configured to start the scheduled task, you will still not be able to find any records in the information_schema.EVENTS table even though you have started the created event. 4|1 Common cycle timing rules ① Cycle execution – keyword EVERY The units are: second, minute, hour, day, week, quarter, month, year, such as: on schedule every 1 second //Execute once per second on schedule every 2 minute //Execute once every two minutes on schedule every 3 day //Execute once every three days ② Execute at a specific time – keyword AT, such as: on schedule at current_timestamp()+interval 5 day // Execute in 5 days on schedule at current_timestamp()+interval 10 minute // Execute in 10 minutes on schedule at '2016-10-01 21:50:00' // Execute at 9:50 PM on October 1, 2016 ③ Execute in a certain time period – Keywords STARTS ENDS, such as: on schedule every 1 day starts current_timestamp()+interval 5 day ends current_timestamp()+interval 1 month //Starts executing every day after 5 days and ends at the end of the next month on schedule every 1 day ends current_timestamp()+interval 5 day //Starts executing every day for 5 days Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: A brief analysis of understanding Vue components from an object-oriented perspective
>>: Implementation of Docker deployment of ElasticSearch and ElasticSearch-Head
If you don't have a Linux system, please refe...
I encountered this problem when I was making the ...
Using the CSS3 border-image property, you can set...
Table of contents 1. Background of WAF 2. What is...
Part.0 Background The company's intranet serv...
The specific method of installing CentOS 7.0 on V...
The transaction log records the operations on the...
The emergence of jQuery has greatly improved our ...
When compiling and installing Nginx, some modules...
The login interface of WeChat applet is implement...
animation-name animation name, can have multiple ...
After the article "This Will Be a Revolution&...
Effect picture: 1. Introduction Your own applet n...
Analyze the execution process. Move the mouse int...
Website, (100-1)% of the content is navigation 1....