MySql 5.6动态分区表基于用户定义的日期

时间:2015-10-01 14:07:21

标签: mysql database

我想创建一个基本的mysql函数,我可以每晚调用它来将表分成两个分区,一个包含给定日期之前的所有内容,另一个包含给定日期之后的所有内容。我有以下内容,但我一直收到此错误。有没有办法可以使用传入日期来分区这个表?

1064. Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed near '), PARTITION p02 VALUES LESS THAN (MAXVALUE)); END' at line 7

DELIMITER $
$

DROP PROCEDURE IF EXISTS UpdateEventsPartitions $$
CREATE PROCEDURE UpdateEventsPartitions (IN `@PartitionDate` datetime)
BEGIN
    DECLARE toDays int;
    SET toDays = TO_DAYS(@PartitionDate);

     ALTER TABLE events REORGANIZE PARTITION p01,p02 INTO (
        PARTITION p01 VALUES LESS THAN (toDays),
        PARTITION p02 VALUES LESS THAN (MAXVALUE));

END $$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我不确定这是否是最好的方法,但我能用这个来解决我的问题。

DELIMITER $$

DROP PROCEDURE IF EXISTS UpdateEventsPartitions $$
CREATE PROCEDURE update_events_partitions (IN `@PartitionDate` datetime)
BEGIN
     SET @sql := CONCAT('ALTER TABLE events REORGANIZE PARTITION p01,p02 INTO ('
                        ,'PARTITION p01 VALUES LESS THAN (TO_DAYS('
                        , CAST((`@PartitionDate`+0) as char(8))
                        ,')),'
                        ,'PARTITION p02 VALUES LESS THAN (MAXVALUE));');

     Select @sql as sqlStatement;
     PREPARE stmt FROM @sql;
     EXECUTE stmt;
     DEALLOCATE PREPARE stmt;

END $$

DELIMITER ;