将mysql表中的分区添加到已分区的表中

wwwo4jvm  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(447)

我的表格如下:

CREATE TABLE TEST_P (
    ID VARCHAR(64),
    F_NAME int NOT NULL,
    L_NAME VARCHAR(64),
    CREATETS DATETIME NOT NULL,
    PRIMARY KEY(DATA_TRACE_ID,CREATETS)
)
PARTITION BY RANGE(DAY(CREATETS))(
        PARTITION day1 VALUES LESS THAN (2),
        PARTITION day2 VALUES LESS THAN (3),
        PARTITION day3 VALUES LESS THAN (4),
        PARTITION day4 VALUES LESS THAN (5),
        PARTITION day5 VALUES LESS THAN (6),
        PARTITION day6 VALUES LESS THAN (7),
        PARTITION day7 VALUES LESS THAN (8),
        PARTITION day8 VALUES LESS THAN (9),
        PARTITION day9 VALUES LESS THAN (10),
        PARTITION day10 VALUES LESS THAN (11),
        PARTITION day11 VALUES LESS THAN (12),
        PARTITION day12 VALUES LESS THAN (13),
        PARTITION day13 VALUES LESS THAN (14),
        PARTITION day14 VALUES LESS THAN (15),
        PARTITION day15 VALUES LESS THAN (16),
        PARTITION day16 VALUES LESS THAN (17),
        PARTITION day17 VALUES LESS THAN (18),
        PARTITION day18 VALUES LESS THAN (19),
        PARTITION day19 VALUES LESS THAN (20),
        PARTITION day20 VALUES LESS THAN (21),
        PARTITION day21 VALUES LESS THAN (22),
        PARTITION day22 VALUES LESS THAN (23),
        PARTITION day23 VALUES LESS THAN (24),
        PARTITION day24 VALUES LESS THAN (25),
        PARTITION day25 VALUES LESS THAN (26),
        PARTITION day26 VALUES LESS THAN (27),
        PARTITION day27 VALUES LESS THAN (28),
        PARTITION day28 VALUES LESS THAN (29),
        PARTITION day29 VALUES LESS THAN (30),
        PARTITION day30 VALUES LESS THAN (31),
        PARTITION day31 VALUES LESS THAN MAXVALUE
);

我想写一个脚本,在今天+1天内删除分区,然后用相同的分区条件为创建一个新分区。例如:
我想删除分区day5,因为我想删除这个表中的数据。
我是这样做的:

ALTER TABLE TEST_P REORGANIZE PARTITION day5 into 
(PARTITION day5 VALUES LESS THAN (5),
    PARTITION day6 VALUES LESS THAN (6),
    PARTITION day7 VALUES LESS THAN (7),
    PARTITION day8 VALUES LESS THAN (8),
    PARTITION day9 VALUES LESS THAN (9),
    PARTITION day10 VALUES LESS THAN (10),
    PARTITION day11 VALUES LESS THAN (11),
    PARTITION day12 VALUES LESS THAN (12),
    PARTITION day13 VALUES LESS THAN (13),
    PARTITION day14 VALUES LESS THAN (14),
    PARTITION day15 VALUES LESS THAN (15),
    PARTITION day16 VALUES LESS THAN (16),
    PARTITION day17 VALUES LESS THAN (17),
    PARTITION day18 VALUES LESS THAN (18),
    PARTITION day19 VALUES LESS THAN (19),
    PARTITION day20 VALUES LESS THAN (20),
    PARTITION day21 VALUES LESS THAN (21),
    PARTITION day22 VALUES LESS THAN (22),
    PARTITION day23 VALUES LESS THAN (23),
    PARTITION day24 VALUES LESS THAN (24),
    PARTITION day25 VALUES LESS THAN (25),
    PARTITION day26 VALUES LESS THAN (26),
    PARTITION day27 VALUES LESS THAN (27),
    PARTITION day28 VALUES LESS THAN (28),
    PARTITION day29 VALUES LESS THAN (29),
    PARTITION day30 VALUES LESS THAN (30),
    PARTITION day31 VALUES LESS THAN (31),
    PARTITION p_default VALUES LESS THAN MAXVALUE)

但我明白了
要重新组织的分区列表出错
这个表增长非常快,所以我想删除一个月前的数据,而不影响表的性能。

ndh0cuux

ndh0cuux1#

滑动时间序列是为数不多的 PARTITIONing . 不幸的是,你的方法是低效的(如果它甚至可以被修复的话)。
取而代之的是,用实际的天数(不是一个月的某一天)每晚添加一个新的分区——按 REORGANIZEing “未来”分为明天和新的“未来”。也, DROP PARTITION 把最老的扔掉。细节在这里。

相关问题