-- 检查事件调度器状态
SHOW VARIABLES LIKE 'event_scheduler';

-- 开启事件调度器
SET GLOBAL event_scheduler = ON;


DROP EVENT IF EXISTS delete_old_records;


DELIMITER //
CREATE EVENT delete_old_records
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
BEGIN
    DELETE FROM tg_callback_log    -- 替换为你的表名
    WHERE create_time < DATE_SUB(NOW(), INTERVAL 2 DAY);   -- 替换为你的时间字段名
END //
DELIMITER ;



-- 查看所有事件
SHOW EVENTS;

-- 查看特定事件详情
SHOW CREATE EVENT delete_old_records;


-- 停止事件
ALTER EVENT delete_old_records DISABLE;

-- 启动事件
ALTER EVENT delete_old_records ENABLE;


-- 首先确保事件调度器是开启的
SET GLOBAL event_scheduler = ON;

-- 创建一个事件，每天执行一次删除操作
CREATE EVENT delete_old_data_by_origin_data
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
  DELETE FROM origin_data
  WHERE create_time < DATE_SUB(NOW(), INTERVAL 48 HOUR);