- 如果有主键,分区键必须是主键
- 查询分区表统计信息
SELECT PARTITION_NAME,SUBPARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_test_p_range_02';
####################
Range分区表
# 创建分区表
create table t_test_p_range_01 (
id int primary key ,
col1 varchar(50),
col2 varchar(50),
col3 int not null)
PARTITION BY RANGE (id) (
partition p1 values less than (10),
partition p2 values less than (20),
partition p3 values less than (30),
partition p4 values less than MAXVALUE
);
INSERT INTO t_test_p_range_01 VALUES (1,'a','a','1');
INSERT INTO t_test_p_range_01 VALUES (11,'a','a','1');
INSERT INTO t_test_p_range_01 VALUES (21,'a','a','1');
INSERT INTO t_test_p_range_01 VALUES (31,'a','a','1');
create table t_test_p_range_02 (
id int not null,
col1 varchar(50),
col2 varchar(50),
col3 int not null)
PARTITION BY RANGE (col3) (
partition p1 values less than (10),
partition p2 values less than (20),
partition p3 values less than (30)
);
INSERT INTO t_test_p_range_02 VALUES (1,'a','a','5');
INSERT INTO t_test_p_range_02 VALUES (2,'a','a','15');
INSERT INTO t_test_p_range_02 VALUES (3,'a','a','25');
INSERT INTO t_test_p_range_02 VALUES (4,'a','a','35'); # error
create index on t_test_p_range_02(col3);
alter table t_test_p_range_02 add partition (partition p4 values less than (40));
create table t_test_p_range_03 (
id int,
col1 varchar(3),
col2 varchar(50),
col3 int)
PARTITION BY RANGE columns (id, col3, col1) (
partition p1 values less than (10,100,'ggg'),
partition p2 values less than (20,200,'nnn'),
partition p3 values less than (30,300,'ttt')
);
INSERT INTO t_test_p_range_03(col1,col2,col3) VALUES ('hhh','a','111');
INSERT INTO t_test_p_range_03(id,col1,col2,col3) VALUES (3,'hhh','a','99');
INSERT INTO t_test_p_range_03(id,col1,col2,col3) VALUES (11,'zzz','a','111');
INSERT INTO t_test_p_range_03(id,col1,col2,col3) VALUES (21,'a','a','222');
INSERT INTO t_test_p_range_03 VALUES (31,'aa','a','99'); #
##############################
list:
- list是int集合。
- 如果是字符类型, 要使用 list columns (col)
create table t_test_p_list_01 (
id int not null,
col1 varchar(50) not null,
col2 varchar(50),
col3 int not null
)
partition by list (col3)(
partition p1 values in (1,2,3,4,5),
partition p2 values in (11,12,13,14,15),
partition p3 values in (21,22,23,24,25)
);
insert into t_test_p_list_01 values (1,'a','a','1');
insert into t_test_p_list_01 values (2,'a','a','11');
insert into t_test_p_list_01 values (3,'a','a','21');
insert into t_test_p_list_01 values (4,'a','a','31');
alter table t_test_p_list_01 add partition (partition p4 values in (31,32,33,34,35));
create table t_test_p_list_02 (
id int not null,
col1 varchar(50) not null,
col2 varchar(50),
col3 int not null
)
partition by list columns (col1)(
partition p1 values in ('a','b','c','d','e'),
partition p2 values in ('f','g','h','i','j'),
partition p3 values in ('k','l','m','n','o')
);
insert into t_test_p_list_02 values (1,'a','a','1');
insert into t_test_p_list_02 values (2,'f','a','11');
insert into t_test_p_list_02 values (3,'k','a','21');
insert into t_test_p_list_02 values (4,'p','a','31');
alter table t_test_p_list_02 add partition (partition p4 values in ('p','q','r','s','t'));
##############################
HASH分区
create table t_test_p_hash_01 (
id int,
col1 varchar(10),
col2 varchar(10),
col3 int
)
PARTITION BY HASH(col3)
PARTITIONS 4;
insert into t_test_p_hash_01 values (1,'a','a','1');
insert into t_test_p_hash_01 values (2,'a','a','2');
insert into t_test_p_hash_01 values (3,'a','a','3');
insert into t_test_p_hash_01 values (4,'a','a','4');
insert into t_test_p_hash_01 values (5,'a','a','5');
insert into t_test_p_hash_01 values (6,'a','a','6');
insert into t_test_p_hash_01 values (5,'a','a','7');
insert into t_test_p_hash_01 values (6,'a','a','8');
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_test_p_hash_01';
create table t_test_p_hash_02 (
id int,
col1 varchar(10),
col2 varchar(10),
col3 int
)
PARTITION BY LINEAR HASH(col3)
PARTITIONS 4;
insert into t_test_p_hash_02 values (1,'a','a','1');
insert into t_test_p_hash_02 values (2,'a','a','2');
insert into t_test_p_hash_02 values (3,'a','a','3');
insert into t_test_p_hash_02 values (4,'a','a','4');
insert into t_test_p_hash_02 values (5,'a','a','5');
insert into t_test_p_hash_02 values (6,'a','a','6');
insert into t_test_p_hash_02 values (5,'a','a','7');
insert into t_test_p_hash_02 values (6,'a','a','8');
insert into t_test_p_hash_02 values ( 9,'a','a', '9');
insert into t_test_p_hash_02 values (10,'a','a','10');
##############################
KEY分区
create table t_test_p_key_01(
id int primary key,
col1 varchar(20),
col2 varchar(20),
col3 int
)
PARTITION BY KEY()
PARTITIONS 3;
insert into t_test_p_key_01 values (1,'a','a','1');
insert into t_test_p_key_01 values (2,'a','a','1');
insert into t_test_p_key_01 values (3,'a','a','1');
insert into t_test_p_key_01 values (4,'a','a','1');
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_test_p_key_01';
##############################
SUBPARTITION
CREATE TABLE t_test_p_sub_01 (
id INT,
col1 varchar(20),
col2 date,
col3 int
)
PARTITION BY RANGE( YEAR(col2) )
SUBPARTITION BY HASH( MONTH(col2) )
SUBPARTITIONS 4 (
PARTITION p1 VALUES LESS THAN (1990),
PARTITION p2 VALUES LESS THAN (2000),
PARTITION p3 VALUES LESS THAN (2010),
PARTITION p4 VALUES LESS THAN MAXVALUE
);
insert into t_test_p_sub_01 values (1, 'a', '1989-01-01',1);
insert into t_test_p_sub_01 values (2, 'a', '1999-02-01',2);
insert into t_test_p_sub_01 values (3, 'a', '2009-03-01',3);
insert into t_test_p_sub_01 values (4, 'a', '2019-04-01',4);
SELECT PARTITION_NAME,SUBPARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_test_p_sub_01';
CREATE TABLE t_test_p_sub_02 (
id INT,
col1 DATE,
col2 varchar(10)
)
PARTITION BY RANGE( YEAR(col1) )
SUBPARTITION BY HASH( TO_DAYS(col1) ) (
PARTITION p0 VALUES LESS THAN (1990) (
SUBPARTITION s0,
SUBPARTITION s1
),
PARTITION p1 VALUES LESS THAN (2000) (
SUBPARTITION s2,
SUBPARTITION s3
),
PARTITION p2 VALUES LESS THAN MAXVALUE (
SUBPARTITION s4,
SUBPARTITION s5
)
);
insert into t_test_p_sub_02 values (1,'1989-01-01','a');
insert into t_test_p_sub_02 values (1,'1999-01-01','a');
insert into t_test_p_sub_02 values (1,'2009-01-01','a');
insert into t_test_p_sub_02 values (1,'1989-05-05','a');
insert into t_test_p_sub_02 values (1,'1999-05-15','a');
insert into t_test_p_sub_02 values (1,'2009-05-25','a');
insert into t_test_p_sub_02 values (1,'1989-09-01','a');
insert into t_test_p_sub_02 values (1,'1999-09-11','a');
insert into t_test_p_sub_02 values (1,'2009-09-21','a');
insert into t_test_p_sub_02 values (1,'2001-09-21','a');
insert into t_test_p_sub_02 values (1,'2003-09-21','a');
insert into t_test_p_sub_02 values (1,'2007-09-21','a');
SELECT PARTITION_NAME,SUBPARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_test_p_sub_02';
alter table t_test_p_sub_02 add PARTITION
(PARTITION p2 VALUES LESS THAN (2010) (
SUBPARTITION s4,
SUBPARTITION s5)
);
##################################################
分区表管理
alter table tablename +
partition_option:
| ADD PARTITION (partition_definition) # 增加分区
| DROP PARTITION partition_names # 删除分区 (数据一同被删除)
| TRUNCATE PARTITION {partition_names | ALL} # 清除分区数据
| COALESCE PARTITION number # 合并分区(只能应用于hash,key)
| REORGANIZE PARTITION [partition_names INTO (partition_definitions)] # 移动分区数据,重新组织分区
| ANALYZE PARTITION {partition_names | ALL} # 分析表分区,收集统计信息
| CHECK PARTITION {partition_names | ALL} # 与对未分区表使用CHECK TABLE几乎相同的方式来检查分区中的错误。
| OPTIMIZE PARTITION {partition_names | ALL} # 优化表分区,回收任何未使用的空间并对分区数据文件进行碎片整理。分析并重建整个表。使用OPTIMIZE PARTITION等效于在该分区上运行CHECK PARTITION,ANALYZE PARTITION和REPAIR PARTITION。
| REBUILD PARTITION {partition_names | ALL} # 重建分区, 这与删除存储在分区中的所有记录,然后重新插入它们的效果相同。 这对于碎片整理很有用。
| REPAIR PARTITION {partition_names | ALL} # 修理损坏的分区
| PARTITION BY partitioning_expression
| REMOVE PARTITIONING # 把分区表修改为不分区