各种分区设计关联的字段必须主键的一部分
或者是主键本身,或者从属主键
求余:key 根据指定的字段进行分区设计
hash根据指定表达式进行分区设计
条件:range 字段/表达式,符合某个条件范围的分区设计
list 字段/表达式,符合某个列表范围的分区设计
key分区算法
create table goods_03(
id int auto_increment,
name varchar(32) not null default ' ',
price int not null default 0,
pubdate datetime not null default '0000-00-00',
primary key(id)
) engine=Myisam charset=utf8
partition by key(id) partitions 10;
hash分区算法
create table goods_hash(
id int auto_increment,
name varchar(32) not null default ' ',
price int not null default 0,
pubdate datetime not null default '0000-00-00',
primary key(id)
) engine=Myisam charset=utf8
partition by hash(month(pubdate)) partitions 10;
如何解决?
create table goods_hash(
id int auto_increment,
name varchar(32) not null default ' ',
price int not null default 0,
pubdate datetime not null default '0000-00-00',
primary key(id,pubdate)
) engine=Myisam charset=utf8
partition by hash(month(pubdate)) partitions 10;
insert into goods_hash values (null ,'apple',5000,'2015-9-23');
insert into goods_hash values (null ,'apple',5000,'2015-1-23');
insert into goods_hash values (null ,'apple',5000,'2015-2-23');
insert into goods_hash values (null ,'apple',5000,'2015-3-23');
insert into goods_hash values (null ,'apple',5000,'2015-4-23');
insert into goods_hash values (null ,'apple',5000,'2015-5-23');
-----分表、分区
------range条件范围分区
-------根据年代给数据包分区
partition by range(字段/表达式)(
partition
)
create table goods_RR(
id int auto_increment,
name varchar(32) not null default ' ',
price int not null default 0,
pubdate datetime not null default '0000-00-00',
primary key(id,pubdate)
) engine=Myisam charset=utf8
partition by range(year(pubdate))(
partition hou70 values less than(1980),
partition hou80 values less than(1990),
partition hou90 values less than(2000),
partition hou00 values less than (2010)
);
insert into goods_RR values (null ,'apple',5000,'2009-9-23');
insert into goods_RR values (null ,'apple',5000,'1949-1-23');
insert into goods_RR values (null ,'apple',5000,'1960-2-23');
insert into goods_RR values (null ,'apple',5000,'1990-3-23');
insert into goods_RR values (null ,'apple',5000,'1999-4-23');
insert into goods_RR values (null ,'apple',5000,'1929-5-23');
根据月份进行分区
公式如下:
partition by range(字段/表达式)(
partition 分区名字 values in(常量),
)
建表
create table goods_LL(
id int auto_increment,
name varchar(32) not null default '',
price int not null default 0,
pubdate datetime not null default '0000-00-00',
primary key(id,pubdate)
) engine =Myisam charset=utf8
partition by list(month(pubdate))(
partition spring values in(3,4,5),
partition summer values in(6,7,8),
partition autumn values in(9,10,11),
partition winter values in(12,1,2)
);
注: spring---春 summer ---夏 autumn---秋 winter---冬
key:该方式区分不明显
hash/range/list会根据业务特点把数据写入到对应的分区表
;