数据表分区学习
一、范围分区
用法:创建一个母表,指定一个列进行范围分区,之后再创建子表,对每个子表划定分区范围,对母表进行数据插入时,不仅会插入到母表,还会根据子表分区范围自动将数据插入到相应的子表 命令:
create table tablename ( 列名1 数据类型,列名2 数据类型) partition by range( 需要分区的列名) ;
create table sontable partition of mothertable for values from ( min) to ( max) ;
举例:
create table testtable ( id int , name text ) partition by range ( id) ;
create table ttable1 partition of testtable for values from ( 0 ) to ( 9 ) ;
create table ttable2 partition of testtable for values from ( 10 ) to ( 19 ) ;
insert into testtable values ( 1 , 'qwe' ) , ( 11 , 'asd' ) ;
数据全部都差入进母表,子表就收取符合范围的数据
注意:范围分区可以一次指定多个列进行分区,eg:对id和age进行范围分区
二、列表分区
用法:创建一个母表后,指定一个列进行列表分区,随后创建子表并规定分区的列的关键字。子表只存储与关键字相同的数据 命令:
CREATE TABLE mothertable ( 列名 数据类型,列名 数据类型) PARTITION BY LIST ( 列表分区的列名) ;
CREATE TABLE sontable PARTITION OF 母表名 FOR VALUES IN ( 关键字) ;
举例
create table testtable ( id int , name text ) partition by list ( name) ;
create table ttable1 partition of testtable for values in ( 'qwe' ) ;
create table ttable2 partition of testtable for values in ( 'asd' ) ;
insert into testtable values ( 1 , 'qwe' ) , ( 2 , 'asd' ) , ( 3 , 'qwe' ) ;
查询母表与子表,母表展示所有数据,子表只存储对应关键字的数据
注意:列表分区只能指定一个列进行列表分区,不能一次指定多列