MySQL 事件无法调用过程来分区数据库



>有没有人遇到过和我一样的问题?请帮忙!!

我有两个过程和一个事件,当我从命令行单独调用它们时,这些过程工作正常。

但是这些过程不能从 mysql 事件内部调用。 以下是我的程序,mysql事件以及如何检查有效结果:

USE ims_db;
DELIMITER;
DROP PROCEDURE IF EXISTS CreatePartition$$
CREATE PROCEDURE CreatePartition(day_keep_data INT)
BEGIN
DECLARE partition_temp TEXT;
DECLARE partition_name CHAR(12);
DECLARE partition_threshold INT DEFAULT 0;
DECLARE day_temp INT;
SET day_temp = day_keep_data;
SET @sql = concat('ALTER TABLE ims_db.ims_counter PARTITION BY RANGE(trigger_time)(');
loop_change_partition: LOOP
IF day_temp <= 0 THEN
LEAVE loop_change_partition;
END IF;
SET day_temp = day_temp - 1;
SET partition_threshold = UNIX_TIMESTAMP() - 86400*day_temp;
SET partition_name = concat('p', CURRENT_DATE() - INTERVAL day_temp DAY + 0);
SET partition_temp = concat('PARTITION', partition_name, ' VALUES LESS THAN (', partition_threshold,'), ');
SET @sql = concat(@sql, partition_temp);
END LOOP loop_change_partition;
SET partition_name = concat('p', CURRENT_DATE() + INTERVAL 1 DAY + 0);
SET partition_temp = concat('PARTITION', partition_name, ' VALUES LESS THAN MAXVALUE);');
SET @sql = concat(@sql, partition_temp);
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
END$$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS DeleteOldData$$
CREATE PROCEDURE DeleteOldData(day_keep_data INT)
BEGIN
SET @sql = concat('DELETE FROM ims_db.ims_counter WHERE ims_db.ims_counter.id > 0
AND ims_db.ims_counter.trigger_time < ', UNIX_TIMESTAMP() - 86400*day_keep_data);
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
ALTER TABLE ims_counter REMOVE PARTITIONING;
END$$
DELIMITER ;
-- DELIMITER $$
-- CREATE FUNCTION ims_daily_work() RETURNS INT(11)
-- BEGIN
--     CALL DeleteOldData(30);
--     CALL CreatePartition(30);
-- END $$
-- DELIMITER ;
DELIMITER $$
CREATE EVENT IMSDailyWork
ON SCHEDULE EVERY 30 SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 10 SECOND
DO
BEGIN
CALL DeleteOldData(30);
CALL CreatePartition(30);
-- SELECT ims_daily_work();
END$$
DELIMITER ;

我如何检查有效结果:

  • 运行命令:显示表状态,如"ims_counter"\G ->检查Create_options:-> 为 null
  • 运行上述所有事件和过程后 ->再次检查Create_options:->它应该被"分区",但我的问题是没有这样的"分区"。

我找到了解决方案:这是因为我的"event_scheduler"已关闭,因此无法启动任何事件。

  1. 要检查事件状态:显示全局变量,如"event_scheduler";
  2. 要启用event_scheduler:设置全局 event_scheduler=1;

相关内容

  • 没有找到相关文章

最新更新