Analysis of the method of setting up scheduled tasks in mysql

Analysis of the method of setting up scheduled tasks in mysql

This article uses an example to describe how to set up scheduled tasks in MySQL. Share with you for your reference, the details are as follows:

Today I encountered a task that needs to be executed regularly every day. Such a function is provided in the MySQL database, so I just organized it and shared it.

1. First check whether the scheduled task is turned on

Check whether the event is enabled:

SHOW VARIABLES LIKE '%event_sche%';

To open the event plan:

SET GLOBAL event_scheduler = 1;

To close the event schedule:

SET GLOBAL event_scheduler = 0;

Close event task:

ALTER EVENT eventName ON COMPLETION PRESERVE DISABLE;

To start the event task:

ALTER EVENT eventName ON COMPLETION PRESERVE ENABLE;

View event tasks:

SHOW EVENTS;

2. Create a stored procedure

DELIMITER //
DROP PROCEDURE IF EXISTS p_test //
CREATE PROCEDURE p_test()
BEGIN
INSERT INTO test(name, create_time) values('testName', now());
END//

3. Set up a scheduled task to call this stored procedure (executed every ten seconds from 1:00 on August 8, 2015)

DROP EVENT IF EXISTS e_test//
CREATE EVENT e_test
ON SCHEDULE EVERY 10 second STARTS TIMESTAMP '2015-08-08 01:00:00'
ON COMPLETION PRESERVE
DO
BEGIN
CALL p_test();
END//

Note: In the event event: ON SCHEDULE scheduled task, there are two ways to set the scheduled task:

1. AT timestamp, used to complete a single scheduled task.

2. EVERY time (unit) quantity time unit [STARTS timestamp] [ENDS timestamp], used to complete repeated scheduled tasks.

In both scheduled tasks, the timestamp can be any TIMESTAMP and DATETIME data type, and the timestamp needs to be greater than the current time.

In a recurring scheduled task, the time (unit) quantity can be any non-null integer, and the time unit is a keyword: YEAR, MONTH, DAY, HOUR, MINUTE or SECOND.

Tip: Other time units are also legal, such as: QUARTER, WEEK, YEAR_MONTH, DAY_HOUR, DAY_MINUTE, DAY_SECOND, HOUR_MINUTE, HOUR_SECOND, MINUTE_SECOND . It is not recommended to use these non-standard time units.

[ON COMPLETION [NOT] PRESERVE]

The ON COMPLETION parameter means "when this event will not occur again", that is, when a single scheduled task is completed or when a repetitive scheduled task reaches the ENDS stage. The function of PRESERVE is to prevent the event from being dropped after execution. It is recommended to use this parameter to view the specific information of EVENT.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • How to configure MySQL scheduled tasks (EVENT events) in detail
  • MySQL scheduled task implementation and usage examples
  • Detailed explanation of mysql scheduled tasks (event events)
  • How to implement Mysql scheduled task backup data under Linux
  • MySQL scheduled task example tutorial
  • How to implement Mysql scheduled tasks under Linux
  • Analysis and solution of the reasons why MySQL scheduled tasks cannot be executed normally

<<:  How to add default time to a field in MySQL

>>:  Vue implements time countdown function

Recommend

About Zabbix forget admin login password reset password

The problem of resetting the password for Zabbix ...

Detailed explanation of commonly used CSS styles (layout)

Compatible with new CSS3 properties In CSS3, we c...

5 cool and practical HTML tags and attributes introduction

In fact, this is also a clickbait title, and it c...

HTML5+CSS3 header creation example and update

Last time, we came up with two header layouts, on...

Web Design TabIndex Element

TabIndex is to press the Tab key to sequentially o...

A great collection of web standards learning resources

These specifications are designed to allow for bac...

Example code for drawing double arrows in CSS common styles

1. Multiple calls to single arrow Once a single a...

How to use Docker containers to implement proxy forwarding and data backup

Preface When we deploy applications to servers as...

Detailed explanation of the use of various MySQL indexes

1. Slow query log 1.1 MySQL log types Logs are us...

Understand the initial use of redux in react in one article

Redux is a data state management plug-in. When us...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

Solution to the problem of mysql service starting but not connecting

The mysql service is started, but the connection ...