MySQL View event execution records


MySQL is a popular relational database management system that provides many functions to help users manage and operate databases. One of them is the EVENT event, which allows users to automatically perform specified actions within a specific time interval, similar to a scheduled task.

In the process of using EVENT, we may need to view EVENT's execution records to understand whether they execute as expected. This article will introduce how to use MySQL to view EVENT execution records and provide some sample code.

1. View EVENT execution records

To view the execution records of EVENT, we can use the information_schema.EVENTS system table to obtain the corresponding information. The EVENTS table contains detailed information about each EVENT, including EVENT name, planned execution time, last execution time, next execution time, etc.

The following is a sample SQL query statement for viewing all EVENT running records:

SELECT EVENT_NAME, LAST_EXECUTED, STATUS
FROM information_schema.EVENTS;

The above query statement will return the name, last execution time and current status (whether enabled) of all EVENTs.

2. Example

Now let us use a specific example to demonstrate how to view the running records of EVENT.

Suppose we have an EVENT named cleanup_logs, which automatically cleans the data in the log table for more than 30 days at 3 am every day. We can create this EVENT using the following code:

CREATE EVENT cleanup_logs
ON SCHEDULE EVERY 1 DAY
STARTS '2022-01-01 03:00:00'
DO
    DELETE FROM logs
    WHERE log_date < DATE_SUB(NOW(), INTERVAL 30 DAY);

The above code creates an EVENT named cleanup_logs, which is executed once every day at 3 a.m. to delete the data in the logs table 30 days ago.

To view the running records of this EVENT, we can run the following query:

SELECT EVENT_NAME, LAST_EXECUTED, STATUS
FROM information_schema.EVENTS
WHERE EVENT_NAME = 'cleanup_logs';

After running the above query, the cleanup_logs EVENT's name, last execution time, and current status will be returned.

3. Conclusion

By using MySQL's information_schema.EVENTS system table, we can easily view EVENT's running records. This is useful for checking and monitoring the execution of automated tasks.

In this article, we provide a simple example to demonstrate how to view EVENT's running records. I hope this example will be helpful to you when using MySQL for daily database management.

Supongo que te gusta

Origin blog.csdn.net/weixin_44816664/article/details/134227407
Recomendado
Clasificación