MySQL scheduled task operation steps and code demonstration

Source: Internet
Author: User

The following articles mainly introduce the actual operation steps of MySQL scheduled tasks (scheduled execution) and the actual application code for implementing MySQL scheduled tasks, the following describes the detailed operation steps. I hope this will help you in this regard.

MySQL is a small and medium database system. Its Event Scheduler, Event sched, is introduced only in MySQL 5.1, which is another feature added in MySQL 5.1, it can be used as a MySQL scheduled task scheduler to replace some timer functions that can only be completed by the operating system task scheduler.

The event scheduler is scheduled to trigger execution, which can also be called a "temporary trigger ". The trigger only executes some statements for the events generated by a table, while the event scheduler executes some statements at a certain interval. Events are managed by a specific thread, that is, the so-called "event scheduler ".

Code

 
 
  1. /* Enable event_scheduler or set it to on */
  2. Set global event_scheduler = 1;
  3. /* Create a simple task and execute it once every minute. You can also insert data from one table to another. For more information, see the following document. Here is a simple example */
  4. Create event MyEvent
  5. On schedule every 1 MINUTE
  6. DO
  7. Insert into list_test (Field 1, Field 1, Field 1, Field 1, Field 1) select Field 1, Field 1, Field 1, Field 1, Field 1, field 1 from table 2 where id = 510;
  8. /* Execute the task at a fixed time every day */
  9. Create event EVENT_ADD_FOR20_ENOUGH
  10. On schedule every 1 DAY
  11. Starts timestamp '2017-12-18 02:58:00 '/× MySQL pay attention to the time zone settings. The default value is not the China Time Zone ×/
  12. DO

SQL statement

 
 
  1. /Files/pochonlee/MySQL-Events-CN.rar 

Add two more sections.

The code used in the actual operations of the MySQL scheduled task is as follows:

 
 
  1. DELIMITER $$  
  2. DROP PROCEDURE IF EXISTS `mystock`.`TEST_KKK`$$  
  3. CREATE PROCEDURE `mystock`.`TEST_KKK`()  
  4. /*LANGUAGE SQL  
  5. | [NOT] DETERMINISTIC  
  6. | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }  
  7. | SQL SECURITY { DEFINER | INVOKER }  
  8. | COMMENT 'string'*/  
  9. BEGIN  
  10. DECLARE NUM INTEGER DEFAULT 0;  
  11. DECLARE $A INT;  
  12. SELECT (20-COUNT(STOCK_CODE)) as number into NUM FROM get_stock_list WHERE FLAG = 0 AND STIME = curdate();  
  13. IF NUM>0 THEN  
  14. PREPARE STMP FROM 'INSERT INTO get_stock_list (stock_code,stock_name,close,raises,stime) 

    select stock_code,stock_name,close,result1,

    selectd from choice_stock where stock_code not in (select stock_code from get_stock_list 

    where stime = curdate() and flag = 0) and selectd = curdate() and selectd = curdate() order by id limit ?' ;  
  15. SET @A = NUM;  
  16. EXECUTE STMP USING @A;  
  17. END IF;  
  18. END$$  
  19. DELIMITER ;  

Code

 
 
  1. DELIMITER $$  
  2. DROP FUNCTION IF EXISTS `mystock`.`FUNCTION_ADD_STOCK_FOR20`$$  
  3. CREATE DEFINER=`root`@`localhost` FUNCTION `FUNCTION_ADD_STOCK_FOR20`() RETURNS int(11)  
  4. BEGIN  
  5. DECLARE NUM,ANOTHER INTEGER;  
  6. SELECT COUNT(STOCK_CODE) as number into NUM FROM get_stock_list WHERE FLAG = 0 AND STIME = curdate();  
  7. if(NUM<20)  
  8. THEN   
  9. SET ANOTHER = 20 - NUM;  
  10. INSERT INTO get_stock_list (stock_code,stock_name,close,raises,stime) select stock_code,

    stock_name,close,result1,selectd from choice_stock where stock_code not in 

    (select stock_code from get_stock_list where stime = curdate() and flag = 0) order by id LIMIT 10;  
  11. END IF;  
  12. RETURN NUM;  
  13. END$$  
  14. DELIMITER ;  

The above content is an introduction to MySQL scheduled tasks. I hope you will get some benefits.

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.