-- 删除表
drop table `Order`;
-- by range 分区
CREATE TABLE `Order` (
`id` INT NOT NULL AUTO_INCREMENT,
`partition_key` INT NOT NULL,
`amt` DECIMAL (5) NULL,
primary key (id,partition_key)
)
PARTITION BY RANGE (partition_key) (
PARTITION part0 VALUES LESS THAN (201901),
PARTITION part1 VALUES LESS THAN (201902),
PARTITION part2 VALUES LESS THAN (201903),
PARTITION part3 VALUES LESS THAN MAXVALUE
);
INSERT INTO `Order` (`id`, `partition_key`, `amt`) VALUES ('1', '201901', '1000');
INSERT INTO `Order` (`id`, `partition_key`, `amt`) VALUES ('2', '201902', '800');
INSERT INTO `Order` (`id`, `partition_key`, `amt`) VALUES ('3', '201903', '1200');
-- 没带上分区字段条件 从全部分区查询
explain partitions select * from `Order` ;
explain partitions select * from `Order` where amt='1000';
-- 带上分区字段 只从分区查询
explain partitions select * from `Order` where partition_key = '201903';
-- 指定分区 只从分区查询
explain partitions select * from `Order` PARTITION(part3);
-- 删除分区和数据
alter table `Order` drop partition part3;
-- 删除数据 保留分区信息
ALTER TABLE `Order` TRUNCATE PARTITION part1;
-- 删除全部分区信息 保留数据
ALTER TABLE `Order` REMOVE PARTITIONING;
-- 重新修改分区
ALTER TABLE `Order`
PARTITION by RANGE(partition_key) (
PARTITION part0 VALUES LESS THAN (201812),
PARTITION part1 VALUES LESS THAN (201912),
PARTITION part2 VALUES LESS THAN (202012),
PARTITION part3 VALUES LESS THAN (202112),
PARTITION part4 VALUES LESS THAN (202212),
PARTITION part5 VALUES LESS THAN MAXVALUE)
;
-- 将最后面part5分区 再细分成3个分区
REORGANIZE PARTITION part5 INTO (
PARTITION p20 VALUES LESS THAN (202312),
PARTITION p21 VALUES LESS THAN (202412),
PARTITION p22 VALUES LESS THAN MAXVALUE
);
-- by hash 分区 partition_key 必须int;
CREATE TABLE `Order` (
`id` INT NOT NULL AUTO_INCREMENT,
`partition_key` INT NOT NULL,
`amt` DECIMAL (5) NULL,
primary key (id,partition_key)
)
PARTITION by hash(partition_key) partitions 100;
-- by key 分区 key可以字符串;
CREATE TABLE `Order` (
`id` INT NOT NULL AUTO_INCREMENT,
`partition_key` INT NOT NULL,
`amt` DECIMAL (5) NULL,
primary key (id,partition_key)
)
PARTITION by key(partition_key) partitions 100;
-- by list 分区 partition_key必须int;
CREATE TABLE `Order` (
`id` INT NOT NULL AUTO_INCREMENT,
`partition_key` INT NOT NULL,
`amt` DECIMAL (5) NULL,
primary key (id,partition_key)
)
PARTITION by list(tenant_id) (
PARTITION part100 VALUES in (100),
PARTITION part10001 VALUES in (10001),
PARTITION part10002 VALUES in (10002)
);