Home  >  Article  >  Database  >  How to use MySQL's event scheduler to implement scheduled tasks

How to use MySQL's event scheduler to implement scheduled tasks

PHPz
PHPzOriginal
2023-08-02 19:16:51843browse

How to use MySQL's event scheduler to implement scheduled tasks

MySQL is a popular relational database management system. In addition to providing powerful data storage and query functions, it also provides an event scheduler. Can be used to implement automatic execution of scheduled tasks. This article will introduce how to use MySQL's event scheduler to implement scheduled tasks, and attach code examples for reference.

  1. Create a scheduled task

First, we need to create a scheduled task and specify the execution time and execution content of the task. The following is an example SQL statement to create a scheduled task:

CREATE EVENT `daily_cleanup`
ON SCHEDULE
    EVERY 1 DAY
    STARTS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
    -- 执行内容
    DELETE FROM `table_name` WHERE `datetime_column` < DATE_SUB(NOW(), INTERVAL 7 DAY);

In the above example, we created a scheduled task named daily_cleanup and set it to execute once a day. The execution time is calculated from one hour after the current time and can be adjusted according to actual needs. The DO keyword is followed by the execution content of the task. Here we use a simple example, which is to delete the data one week before the specified date in the table_name table.

  1. Enable event scheduler

In MySQL, the event scheduler is not enabled by default, we need to enable it manually. Just execute the following SQL statement:

SET GLOBAL event_scheduler = ON;
  1. View scheduled tasks

After enabling the event scheduler, we can view the scheduled tasks that have been created. Execute the following SQL statement:

SHOW EVENTS;

This will list the relevant information of all created scheduled tasks, including task name, execution time, execution content, etc.

  1. Modify scheduled tasks

If you need to modify the scheduled tasks that have been created, we can use the following SQL statement:

ALTER EVENT `event_name`
ON SCHEDULE
    EVERY 2 DAY
    STARTS CURRENT_TIMESTAMP + INTERVAL 2 HOUR
DO
    -- 新的执行内容
    UPDATE `table_name` SET `column_name` = 'new_value' WHERE `id` = 1;

In the above example, we modify A scheduled task named event_name was created, its execution time was changed to once every two days, and the execution content was also modified accordingly.

  1. Delete scheduled tasks

If you need to delete a scheduled task that has been created, you can use the following SQL statement:

DROP EVENT `event_name`;

After executing the above statement, event_nameThe corresponding scheduled task will be deleted.

Summary:

Through MySQL's event scheduler, we can easily implement automatic execution of scheduled tasks. Just create a scheduled task, set its execution time and execution content, and then enable the event scheduler. By viewing, modifying and deleting scheduled tasks, we can flexibly manage and adjust scheduled tasks. I hope this article will help you understand and use MySQL's event scheduler.

Reference materials:

  1. MySQL official documentation: https://dev.mysql.com/doc/refman/8.0/en/events.html
  2. MySQL Introduction to event scheduler: https://www.cnblogs.com/xiehongfeng100/p/11739503.html

The above is the detailed content of How to use MySQL's event scheduler to implement scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn