MySQL scheduled task implementation and usage examples

MySQL scheduled task implementation and usage examples

This article uses examples to illustrate the implementation and use of MySQL scheduled tasks. Share with you for your reference, the details are as follows:

MySQL 5.1.6 adds an event scheduler that can perform scheduled tasks (scheduled deletion of records, scheduled data statistics), replacing the previous system's scheduled tasks. The MySQL event scheduler can execute a task accurately per second.

The difference between event scheduler and trigger: event scheduler triggers the execution of certain tasks based on a specific time period, while trigger is triggered based on the events generated by a certain table.

1. Check whether it is turned on

> show variables like 'event_scheduler';

2. Enable the event scheduler

set global event_scheduler = on;

The settings here will be automatically closed when MySQL is restarted. If you need to keep it turned on, you need to configure it in my.ini as follows:

event_scheduler = on

3. Create event syntax

CREATE EVENT [IF NOT EXISTS ] event_name
ON SCHEDULE schedule
[ ON COMPLETION [ NOT ] PRESERVE ]
[ ENABLE | DISABLE ]
[ COMMENT 'Comment' ]
DO SQL statement;
 
schedule : AT TIMESTAMP [ + INTERVAL interval ] | EVERY interval [ STARTS TIMESTAMP ] [ ENDS TIMESTAMP ]
interval : quantity { YEAR | QUARTER | MONTH | DAY |
           HOUR | MINUTE | WEEK | SECOND |
           YEAR_MONTH | DAY_HOUR | DAY_MINUTE | DAY_SECOND |
           HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND }

event_name: event name, with a maximum length of 64 characters.

schedule: execution time.

[ON COMPLETION [NOT] PRESERVE]: Whether the event needs to be reused.

[ ENABLE | DISABLE ] : Enable or disable the event.

4. Closing Events

ALTER EVENT event_name DISABLE;

5. Opening events

ALTER EVENT event_name ENABLE;

6. Deleting events

DROP EVENT [IF EXISTS ] event_name;

7. View all events

SHOW EVENTS;

8. Event Examples

Let's create a simple test table for testing.

CREATE TABLE `test` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
 `now` datetime DEFAULT NULL COMMENT 'time',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

There are two types of events, one is interval triggering and the other is specific time triggering.

We insert a record into the test table every second:

DROP EVENT IF EXISTS event_test;
CREATE EVENT event_test
ON SCHEDULE EVERY 1 SECOND STARTS '2017-08-22 11:57:00' ENDS '2017-08-22 12:00:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Insert records into the test table every second'
DO INSERT INTO test VALUES(NULL, now());

We specify the time to insert a record into the test table:

DROP EVENT IF EXISTS event_test2;
CREATE EVENT event_test2
ON SCHEDULE AT '2017-08-22 12:01:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Insert records into the test table at specified time'
DO INSERT INTO test VALUES(999999, now());

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
  • Analysis of the method of setting up scheduled tasks in mysql
  • 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

<<:  The process of using vxe-table to make editable tables in vue

>>:  Linux file management command example analysis [display, view, statistics, etc.]

Recommend

How to set the memory size of Docker tomcat

When installing Tomcat in Docker, Tomcat may over...

Detailed explanation of Kubernetes pod orchestration and lifecycle

Table of contents K8S Master Basic Architecture P...

MySQL index for beginners

Preface Since the most important data structure i...

How to use the yum command

1. Introduction to yum Yum (full name Yellow dogU...

No-nonsense quick start React routing development

Install Enter the following command to install it...

MySQL data compression performance comparison details

Table of contents 1. Test environment 1.1 Hardwar...

Installing MySQL 8.0.12 based on Windows

This tutorial is only applicable to Windows syste...

HTML realizes real-time monitoring function of Hikvision camera

Recently the company has arranged to do some CCFA...

JS implementation of Apple calculator

This article example shares the specific code of ...

Web design tips on form input boxes

1. Dashed box when cancel button is pressed <br...