1. 程式人生 > >mysql 分割槽表 range分割槽

mysql 分割槽表 range分割槽

首先呢我們來看下怎麼建立一個分割槽表

在上節課的時候 我們也說過 在分割槽的時候
如果分割槽欄位中有主鍵或者唯一索引的列,那麼多有主鍵列和唯一索引列都必須包含進來。

1 ,按照年齡的範圍
create table staff_r12(
id int not null auto_increment,
name varchar(40) not null,
age int not null,
primary key (id,age)
)engine=myisam default charset=utf8
partition by range(age)(
partition age_1 values less than(20),
partition age_2 values less than(40),
partition age_3 values less than maxvalue
);

2,按照時間戳timestamp範圍來分割槽

create table staff_r13(
id int not null,
staff_status varchar(20) not null,
report_updated timestamp not null default current_timestamp on update current_timestamp
)
partition by range(unix_timestamp(report_updated))(
partition p0 values less than (unix_timestamp(‘2008-01-01 00:00:00’)),
partition p1 values less than (unix_timestamp(‘2008-04-01 00:00:00’)),
partition p2 values less than (unix_timestamp(‘2008-07-01 00:00:00’)),
partition p3 values less than (unix_timestamp(‘2008-10-01 00:00:00’)),
partition p9 values less than maxvalue
);

6.1.3、根據DATE、DATETIME範圍
mysql不支援在日期型別上面直接建立分割槽,需要藉助函式來建立
create table staff_14(
name varchar(25) not null,
age int not null,
joined date not null
)
partition by range columns(joined)(
partition p0 values less than (‘1960-01-01’),
partition p1 values less than (‘1970-01-01’),
partition p2 values less than (‘1980-01-01’),
partition p3 values less than (‘1990-01-01’),
partition p4 values less than maxvalue
);
6.1.4、RANGE分割槽在如下場合特別有用

create table staff_15(
id int not null,
name varchar(30),
joined date not null default ‘9999-12-31’
)engine=myisam default charset=utf8
partition by range(year(joined))(
partition p0 values less than (2016),
partition p1 values less than (2017),
partition p4 values less than MAXVALUE
);