mysql的partition

一 、建表时直接建分区

CREATE TABLE `yigan_subdivision_third_indicator_collect_day` (
    `id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增ID',
    `hierarchy_code` VARCHAR ( 50 ) NOT NULL DEFAULT '' COMMENT '分部、点部、站点、地区、大区、全国 层级代码(全国001)',
    `hierarchy_name` VARCHAR ( 100 ) NOT NULL DEFAULT '' COMMENT '分部、点部、站点、地区、大区、全国 层级名称',
    `inc_day` INT ( 12 ) NOT NULL DEFAULT '0' COMMENT '分区时间',
    PRIMARY KEY ( `id`, `inc_day` )
    ) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT = '壹杆项目结果表-精细化指标-日' /*!50100 PARTITION BY RANGE ( inc_day ) (
        PARTITION p20220301
        VALUES
        LESS THAN ( 20220302 ) ENGINE = InnoDB,
        PARTITION p20220302
        VALUES
        LESS THAN ( 20220303 ) ENGINE = InnoDB,
        PARTITION p20220303
        VALUES
        LESS THAN ( 20220304 ) ENGINE = InnoDB
) */

二、建完表后分区

该sql是指原来就没有分区,如果有分区的话,下面的语句会将原有分区全部删除,在新建语句中的分区

ALTER TABLE `tb_member_login_logs` PARTITION BY RANGE(days) (
    PARTITION `p197001` VALUES LESS THAN (19700101),
    PARTITION `p202003` VALUES LESS THAN (20200301),
    PARTITION `p202004` VALUES LESS THAN (20200401),
    PARTITION `p202005` VALUES LESS THAN (20200501),
    PARTITION `p202006` VALUES LESS THAN (20200601),
    PARTITION `p202007` VALUES LESS THAN (20200701),
    PARTITION `p202008` VALUES LESS THAN (20200801),
    PARTITION `p202009` VALUES LESS THAN (20200901),
    PARTITION `p202010` VALUES LESS THAN (20201001),
    PARTITION `p202011` VALUES LESS THAN (20201101),
    PARTITION `p202012` VALUES LESS THAN (20201201),
    PARTITION `p202101` VALUES LESS THAN (20210101),
    PARTITION `p202102` VALUES LESS THAN (20210201),
    PARTITION `p202103` VALUES LESS THAN (20210301)
);

三、新增分区

ALTER TABLE kb_pd_pickup ADD PARTITION (
    PARTITION `p202103` VALUES LESS THAN ( 202231 ),
    PARTITION `p202105` VALUES LESS THAN ( 202233 ) 
);

注意:分区表是按范围进行分区,其中,分区顺序是严格地进单调递增。就是说,如果想在中间新增分区是不可行的,只能按照顺序进行递增

比如,kb_pd_pickup表已经存在p202103和p202105俩个分区了,这时执行一下sql

ALTER TABLE kb_pd_pickup ADD PARTITION (
    PARTITION `p202104` VALUES LESS THAN ( 202232),
);

这时就会报错VALUES LESS THAN value must be strictly increasing for each partition。

解决办法也就只能删除后面的分区,在重新建分区,删除分区的时候也会数据全部删除,慎重。

四、查询时强制指定分区

select *  from t_recharge_logs PARTITION(p202109,p202110,p202111) where id =1;

五、删除分区数据同时在新建分区(truncate)

alter table kb_pd_pickup truncate partition `p202104`

六、删除分区数据(drop)

alter table kb_pd_pickup drop partition `p202104`

你可能感兴趣的:(mysql的partition)