子分区要求:
- 子分区是分区表中每个分区的再次分割
- 子分区只能是HASH或者KEY分区
- 子分区可以用于特别大的表,在多个磁盘间分配数据和索引
- 每个分区必须有相同数量的子分区
- 如果在一个分区上的任何分区上使用SUBPARTITION 来明确定义任何子分区,那么就必须定义所有的子分区
创建子分区
子分区由两种创建方法,一种是不定义每个子分区子分区的名字和路径由分区决定,二是定义每个子分区的分区名和各自的路径
1.不定义每个子分区
CREATE TABLE tb_sub (id INT, purchased DATE)
PARTITION BY RANGE( YEAR(purchased) )
SUBPARTITION BY HASH( TO_DAYS(purchased) )
SUBPARTITIONS 2 (
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (2000),
PARTITION p2 VALUES LESS THAN MAXVALUE
);
SELECT PARTITION_NAME,PARTITION_METHOD,PARTITION_EXPRESSION,PARTITION_DESCRIPTION,TABLE_ROWS,SUBPARTITION_NAME,SUBPARTITION_METHOD,SUBPARTITION_EXPRESSION
FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA=SCHEMA() AND TABLE_NAME='tb_sub';
2.定义每个子分区
定义子分区可以为每个子分区定义具体的分区名和分区路径
CREATE TABLE tb_sub_ev (id INT, purchased DATE)
PARTITION BY RANGE( YEAR(purchased) )
SUBPARTITION BY HASH( TO_DAYS(purchased) ) (
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
)
);
3.测试数据
INSERT INTO tb_sub_ev() VALUES(1,'1989-01-01'),(2,'1989-03-19'),(3,'1989-04-19');
当往里面插入三条记录时,其中‘1989-01-01’和‘1989-04-19’存储在p0_s0分区中,‘1989-03-19’存储在p0_s1当中