Implementation of MySQL scheduled tasks and its actual application code

Source: Internet
Author: User

The following articles mainly introduce the actual operation process of MySQL scheduled tasks (scheduled execution operations) and the implementation of MySQL scheduled tasks (scheduled execution operations) we will use the description of the Code in the process. The following describes the specific solution and hope it will be helpful for your future study.

MySQL scheduled task (scheduled execution operation) Mysql is a small and medium database system, its Event Scheduler, Event Scheduler, introduced the Event Scheduler only in mysql 5.1. It is another feature added in MySQL 5.1 and can be used as a scheduled task Scheduler, this replaces 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

Enable event_scheduler or on

 
 
  1. set global event_scheduler =1; 

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.

 
 
  1. CREATE EVENT MyEvent  
  2. ON SCHEDULE EVERY 1 MINUTE   
  3. DO 

Nsert 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;

Daily scheduled execution

 
 
  1. CREATE EVENT EVENT_ADD_FOR20_ENOUGH  
  2. ON SCHEDULE EVERY 1 DAY   
  3. STARTS TIMESTAMP '2009-12-18 02:58:00'/×MYSQL 

Note the time zone settings. The default value is not the China time zone.

DO

SQL statement

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

MySQL scheduled task code

 
 
  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) s
    elect 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 (scheduled execution operations). I hope you will gain 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.