赞
踩
就是将一个表按照条件分割成若干个小表(并不是物理表,而是一个分区,每个分区对应一个文件),将表从一个大的数据文件分成了若干个小的数据文件;mysql5.1后支持分区
这就很简单了,提高查询速度的目的较多。还有方便管理,提高最大存储量,提高吞吐量等
- create table t1 (id int,age int,name varcher(20),pos_date datetime)
- pratition by range(age)
- (
- pratition p01 values less than (10),
- pratition p01 values less than (20),
- pratition p01 values less than (30),
- pratition p01 values less than (maxvalues)
- );
- create table t1 (id int,cidint,name varcher(20),pos_date datetime)
- pratition by list(cid)
- (
- pratition p01 values less than (1,4,7),
- pratition p01 values less than (2,5,8),
- pratition p01 values less than (3,6,9),
- );
- create table t3 (id int,age int,name varcher(20),pos_date datetime)
- pratition by hash(cid)
- pratitions 4;
线性哈希:使用一个线性2的幂;有利于存储大量数据,增删合拆更快,但是分布不均匀。
语法:同上hash前面加linear
- create table t3 (id int,age int,name varcher(20),pos_date datetime)
- pratition by key(cid)
- pratitions 4;
- create table t1 (a int,b int,c int)
- pratition by range columns(a ,b)
- (
- pratition p01 values less than (10,10),
- pratition p01 values less than (10,20),
- pratition p01 values less than (10,30),
- pratition p01 values less than (10,maxvalues)
- pratition p01 values less than (maxvalues,maxvalues)
- );
通俗说就是子分区就是在表分区后再进行分区
- create table t4 (id int, udate date)
- partition by range(year (udate))
- subpartition by hash (to_days(udate))
- subpartitions 2
- (
- partition p01 values less than(1990),
- partition p01 values less than(20000),
- partition p01 values less than(maxvalues)
- );
注意:每个分区的子分区数量必须一致
- 修改分区
- alter table t1 partition by range(age)
- (
- partition p01 values less than(10),
- partition p01 values less than(20),
- )
-
- 删除分区(包括数据)
- alter table t1 drop partition p02;
- 删除分区的数据
- alter table t1 truncate partition p01;
-
- 新增分区(只能从高端上加)
- alter table access_log add partition(
- partition p4 values less than (to_days('20190105'))
- );
-
- 拆分分区
- alter table access_log reorganize partition p4 into(
- -> partition s0 values less than(to_days('20190104')),
- -> partition s1 values less than(to_days('20190105'))
- -> );
-
- 合并分区
- alter table access_log reorganize partition s0,s1 into (
- partition p4 values less than (to_days('20190105'))
- );
当分区建立后,分区需持续关心。后续可能出现各种状况,可以进行相应的重建、优化、修复等操作
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。