按范围分区(range)
示例:
创建分区表,插入数据
mysql> use test;
mysql> create table topic(
-> tid int primary key auto_increment,
-> title char(20) not null default ''
-> )engine myisam charset utf8
-> partition by range(tid)(
-> partition t0 values less than(10),
-> partition t1 values less than(20),
-> partition t2 values less than(MAXVALUE)
-> );
mysql> insert into topic(title) values('a');
查看表文件
[root@localhost test]# cd /www/server/data/test [root@localhost test]# ll ... -rw-rw---- 1 mysql mysql 8584 5月 10 11:54 t8.frm -rw-rw---- 1 mysql mysql 0 5月 10 11:54 t8.MYD -rw-rw---- 1 mysql mysql 1024 5月 10 11:54 t8.MYI -rw-rw---- 1 mysql mysql 8618 5月 10 11:57 t9.frm -rw-rw---- 1 mysql mysql 104 5月 10 13:45 t9.MYD -rw-rw---- 1 mysql mysql 4096 5月 19 10:38 t9.MYI -rw-rw---- 1 mysql mysql 8590 6月 17 13:53 topic.frm -rw-rw---- 1 mysql mysql 32 6月 17 13:53 topic.par -rw-rw---- 1 mysql mysql 65 6月 17 13:54 topic#P#t0.MYD -rw-rw---- 1 mysql mysql 2048 6月 17 13:54 topic#P#t0.MYI -rw-rw---- 1 mysql mysql 0 6月 17 13:53 topic#P#t1.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 13:53 topic#P#t1.MYI -rw-rw---- 1 mysql mysql 0 6月 17 13:53 topic#P#t2.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 13:53 topic#P#t2.MYI -rw-rw---- 1 mysql mysql 16528 5月 20 14:34 user.frm -rw-rw---- 1 mysql mysql 10485760 5月 20 14:34 user.ibd
可以看到数据插入到了 topic#P#t0.MYD 文件
把数据存入t1分区
mysql> insert into topic(tid,title) values(11,'b');
可以看到数据存入到了 topic#P#t1.MYD 文件
[root@localhost test]# ll ... -rw-rw---- 1 mysql mysql 65 6月 17 13:54 topic#P#t0.MYD -rw-rw---- 1 mysql mysql 2048 6月 17 13:54 topic#P#t0.MYI -rw-rw---- 1 mysql mysql 65 6月 17 14:06 topic#P#t1.MYD -rw-rw---- 1 mysql mysql 2048 6月 17 14:06 topic#P#t1.MYI -rw-rw---- 1 mysql mysql 0 6月 17 13:53 topic#P#t2.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 13:53 topic#P#t2.MYI
按散落的点分区(list)
示例:
mysql> create table area( aid int, zone char(6))engine myisam charset utf8;
mysql> insert into area values(1,'bj'),(2,'sh'),(3,'hn'),(4,'ah');
mysql> create table users(
-> uid int,
-> uname char(6),
-> aid int
-> )engine myisam charset utf8
-> partition by list(aid)(
-> partition bj values in(1),
-> partition sh values in(2),
-> partition hn values in(3),
-> partition ah values in(4)
-> );
mysql> insert into users(uname,aid) values('zhangsan',1);
mysql> insert into users(uname,aid) values('lisi',2);
mysql> insert into users(uname,aid) values('wangwu',9);
ERROR 1526 (HY000): Table has no partition for value 9
[root@localhost test]# ll ... -rw-rw---- 1 mysql mysql 8618 6月 17 14:46 users.frm -rw-rw---- 1 mysql mysql 0 6月 17 14:46 users#P#ah.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 14:46 users#P#ah.MYI -rw-rw---- 1 mysql mysql 32 6月 17 14:46 users.par -rw-rw---- 1 mysql mysql 54 6月 17 14:55 users#P#bj.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 14:55 users#P#bj.MYI -rw-rw---- 1 mysql mysql 0 6月 17 14:46 users#P#hn.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 14:46 users#P#hn.MYI -rw-rw---- 1 mysql mysql 27 6月 17 14:55 users#P#sh.MYD -rw-rw---- 1 mysql mysql 1024 6月 17 14:55 users#P#sh.MYI
MYSQL的分区主要有两种形式:水平分区和垂直分区
水平分区
RANGE 分区:基于属于一个给定连续区间的列值,把多行分配给分区。
LIST分区:类似于按RANGE分区,区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择。
HASH分区:基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的这些行的列值进行计算。这个函数可以包含MySQL中有效的、产生非负整数值的任何表达式。
KEY分区:类似于按HASH分区,区别在于KEY分区只支持计算一列或多列,且MySQL服务器提供其自身的哈希函数。必须有一列或多列包含整数值。
复合分区:基于RANGE/LIST 类型的分区表中每个分区的再次分割。子分区可以是 HASH/KEY等类型。
垂直分区
这种分区方式一般来说是通过对表的垂直划分来减少目标表的宽度,使某些特定的列被划分到特定的分区,每个分区都包含了其中的列所对应所有行。



