mysql事件无法调用过程对数据库进行分区

nnsrf1az  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(471)

有人遇到过和我一样的问题吗?请帮忙!!
我有两个过程和一个事件,当我从命令行分别调用它们时,这些过程工作得很好。
但是这些过程不能从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\u counter”\g->检查 Create_options: ->它是空的
在运行完上述所有事件和过程之后,再次检查 Create_options: ->它应该是分区的,但我的问题是没有这样的分区。

7lrncoxx

7lrncoxx1#

我找到了解决方法:这是因为我的“事件调度程序”已关闭,因此无法启动任何事件。
检查事件状态:显示全局变量,如“event_scheduler”;
启用事件调度程序:设置全局事件调度程序=1;

相关问题