千万级数据新增主键及对表分区

有可能会锁表,请在业务量低峰处理。表数据量33327402条

新增主键
ALTER TABLE treasurebox.deduction DROP PRIMARY KEY, ADD PRIMARY KEY (ID, DATE_CT);

修改期间发现mysql所在的磁盘空间急剧上升
千万级数据新增主键及对表分区_第1张图片
千万级数据新增主键及对表分区_第2张图片

网上描述为正常现场,维护索引本来就是个很大的内存开销,它需要给每行数据建立新的索引文件,扫描10G左右的数据量飙升很正常,上升了约12G。有大佬可以帮忙解读下。感谢!!

为啥要新建主键,因为分区的字段必须要为主键。
接下来开始新建分区(一个季度分为一个区,按需拆分,目前数据文件控制在1G左右)
ALTER TABLE treasurebox.deduction PARTITION BY RANGE(DATE_CT)PARTITIONS 28(
PARTITION p0 VALUES LESS THAN(20150101)ENGINE = INNODB,
PARTITION p1 VALUES LESS THAN(20150401)ENGINE = INNODB,
PARTITION p2 VALUES LESS THAN(20150701) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN(20151001) ENGINE = InnoDB,
PARTITION p4 VALUES LESS THAN(20160101) ENGINE = InnoDB,
PARTITION p5 VALUES LESS THAN(20160401) ENGINE = InnoDB,
PARTITION p6 VALUES LESS THAN(20160701) ENGINE = InnoDB,
PARTITION p7 VALUES LESS THAN(20161001) ENGINE = InnoDB,
PARTITION p8 VALUES LESS THAN(20170101) ENGINE = InnoDB,
PARTITION p9 VALUES LESS THAN(20170401) ENGINE = InnoDB,
PARTITION p10 VALUES LESS THAN(20170701) ENGINE = InnoDB,
PARTITION p11 VALUES LESS THAN(20171001) ENGINE = InnoDB,
PARTITION p12 VALUES LESS THAN(20180101) ENGINE = InnoDB,
PARTITION p13 VALUES LESS THAN(20180401) ENGINE = InnoDB,
PARTITION p14 VALUES LESS THAN(20180701) ENGINE = InnoDB,
PARTITION p15 VALUES LESS THAN(20181001) ENGINE = InnoDB,
PARTITION p16 VALUES LESS THAN(20190101) ENGINE = InnoDB,
PARTITION p17 VALUES LESS THAN(20190401) ENGINE = InnoDB,
PARTITION p18 VALUES LESS THAN(20190701) ENGINE = InnoDB,
PARTITION p19 VALUES LESS THAN(20191001) ENGINE = InnoDB,
PARTITION p20 VALUES LESS THAN(20200101) ENGINE = InnoDB,
PARTITION p21 VALUES LESS THAN(20200401) ENGINE = InnoDB,
PARTITION p22 VALUES LESS THAN(20200701) ENGINE = InnoDB,
PARTITION p23 VALUES LESS THAN(20201001) ENGINE = InnoDB,
PARTITION p24 VALUES LESS THAN(20210101) ENGINE = InnoDB,
PARTITION p25 VALUES LESS THAN(20210401) ENGINE = InnoDB,
PARTITION p26 VALUES LESS THAN(20210701) ENGINE = InnoDB,
PARTITION p27 VALUES LESS THAN(20211001) ENGINE = InnoDB
);

约半小时执行完毕,原分区表9G+。

你可能感兴趣的:(数据库,mysql)