What is event scheduling in MySQL?

Answer

MySQL's Event Scheduler is a built-in job scheduler that executes SQL statements or stored procedures on a scheduled basis — similar to cron jobs but running entirely within MySQL. Enable it: SET GLOBAL event_scheduler = ON; or in my.cnf: event_scheduler = ON. Creating events: CREATE EVENT purge_old_logs ON SCHEDULE EVERY 1 DAY STARTS "2024-01-01 03:00:00" DO DELETE FROM audit_logs WHERE created_at < DATE_SUB(NOW(), INTERVAL 90 DAY);. One-time event: ON SCHEDULE AT NOW() + INTERVAL 1 HOUR. View events: SHOW EVENTS;. Modify: ALTER EVENT name DISABLE;. Drop: DROP EVENT name;. Use cases: (1) Archiving old data to a history table; (2) Purging expired sessions or tokens; (3) Refreshing summary/aggregate tables for reporting; (4) Sending scheduled notifications; (5) Database maintenance tasks (ANALYZE TABLE, OPTIMIZE TABLE). Advantages over application-level cron: runs even if the application is down; no external scheduler to maintain. Disadvantages: event code lives in the database (harder to version control); failures are less visible (check mysql.event_errors); not suitable for complex logic better handled in application code. Always log event execution for monitoring.