Event Performance
Events run in their own thread. Long-running events can block other operations. Design events to be fast and transactional, running in off-peak hours.
Events run in their own thread. Long-running events can block other operations. Design events to be fast and transactional, running in off-peak hours.
-- Check event thread activity
SELECT * FROM performance_schema.threads WHERE name LIKE "%event_scheduler%";
-- Batch delete to avoid long transactions and I/O spikes
DELIMITER //
CREATE EVENT batch_purge
ON SCHEDULE EVERY 5 MINUTE
DO
BEGIN
DELETE FROM logs WHERE created_at < NOW() - INTERVAL 90 DAY LIMIT 5000;
END//
DELIMITER ;
-- Schedule heavy work during off-peak hours (2-4 AM)
ALTER EVENT weekly_report ON SCHEDULE EVERY 1 WEEK STARTS "2024-01-07 02:00:00";
Delete in small batches (LIMIT 1000-5000) to avoid long lock waits and replication lag spikes.