Home > Database > Mysql Tutorial > How to Schedule a Stored Procedure to Run Regularly in MySQL?

How to Schedule a Stored Procedure to Run Regularly in MySQL?

Susan Sarandon
Release: 2024-11-04 08:39:30
Original
338 people have browsed it

How to Schedule a Stored Procedure to Run Regularly in MySQL?

How to Schedule a Routine in MySQL

MySQL allows you to schedule stored procedures to run at specified intervals. This can be useful for automating tasks such as data cleanup or regular reports. To schedule a stored procedure, you can use the CREATE EVENT statement.

Scheduling a Stored Procedure to Delete Old Data

Consider the following stored procedure (delete_rows_links) which deletes activation links that are older than one day:

<code class="sql">DROP PROCEDURE IF EXISTS `delete_rows_links` 
GO

CREATE PROCEDURE delete_rows_links
BEGIN 

    DELETE activation_link
    FROM activation_link_password_reset
    WHERE  TIMESTAMPDIFF(DAY, `time`, NOW()) < 1 ; 

END 

GO</code>
Copy after login

To schedule this stored procedure to run every 5 seconds, you can use the following CREATE EVENT statement:

<code class="sql">CREATE EVENT myevent
    ON SCHEDULE EVERY 5 SECOND
    DO
      CALL delete_rows_links();</code>
Copy after login

This event will cause the delete_rows_links stored procedure to be executed every 5 seconds. You can find more examples of event scheduling in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/create-event.html

The above is the detailed content of How to Schedule a Stored Procedure to Run Regularly in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template