1、关于Mysql分区技术的简介
Mysql的分区技术不同于之前的分表技术,它与水平分表有点类似,但是它是在罗继层进行的水平分表,对于应用程序而言它还是一张表,Mysql5.1有四种分区类型:
1、RANGE分区:基于属于一个给定连续区间的列值,把多行分配给分区.
2、LIST分区:类似于按RANGE分区,区别在于LIST分区是基于列值分配一个离散值集合中的某个值来进行选择
3、HASH分区:基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的这些行的列值来进行计算,这个函数可以包含Mysql中有效的、产生非负整数值的任何表达式
4、KEY分区:类似于按HASH分区,区别在于KEY分区只支持计算一列或多列,且Mysql服务器提供其自身的哈希哈数
主要使用的是,RANGE和LIST分区技术.
2、关于Mysql的RANGE分区技术的小案例
1、按简单的id编号进行数据的分区
create table employees ( id int ) partition by <strong><span style="color:#990000;">range(id) </span></strong> ( partition p0 values less than (6) , partition p1 values less than (11) , partition p2 values less than (16) , partition p3 values less than (21) ); insert into employees values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); insert into employees values(11),(12),(13),(14),(15),(16),(17),(18),(19),(20);到Mysql的数据库里面可以查看到表employees的分区文件
-rw-rw---- 1 mysql mysql 8556 Apr 7 03:58 employees.frm -rw-rw---- 1 mysql mysql 32 Apr 7 03:58 employees.par -rw-rw---- 1 mysql mysql 98304 Apr 7 04:00 employees#P#p0.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:00 employees#P#p1.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:00 employees#P#p2.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:00 employees#P#p3.ibd
create table employees_2 ( hired date ) partition by<strong><span style="color:#ff0000;"> range( year(hired) ) </span></strong> ( partition p0 values less than (1991) , partition p1 values less than (1996) , partition p2 values less than (2001) , partition p3 values less than MAXVALUE ); insert into employees values('1990-01-01'),('1991-01-01'),('1992-01-01'),('1993-01-01'),('1997-01-01'), ('1998-01-01'),('2002-01-01'),('2003-01-01'),('2004-01-01'),('2005-01-01'); -rw-rw---- 1 mysql mysql 8562 Apr 7 04:11 employees_2.frm -rw-rw---- 1 mysql mysql 32 Apr 7 04:11 employees_2.par -rw-rw---- 1 mysql mysql 98304 Apr 7 04:12 employees_2#P#p0.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:12 employees_2#P#p1.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:12 employees_2#P#p2.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:12 employees_2#P#p3.ibd
3、关于Mysql的LIST分区技术的小案例
create table employees_3 ( id int ) partition by <span style="color:#ff0000;"><strong>list(id) </strong></span> ( partition p0 values in (3,5,6,9,17) , partition p1 values in (1,2,10,11,19,20) , partition p2 values in (4,12,13,14,18) , partition p3 values in (7,8,15,16) ); insert into employees_3 values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); insert into employees_3 values(11),(12),(13),(14),(15),(16),(17),(18),(19),(20); -rw-rw---- 1 mysql mysql 8556 Apr 7 04:18 employees_3.frm -rw-rw---- 1 mysql mysql 32 Apr 7 04:18 employees_3.par -rw-rw---- 1 mysql mysql 98304 Apr 7 04:18 employees_3#P#p0.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:18 employees_3#P#p1.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:18 employees_3#P#p2.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:18 employees_3#P#p3.ibd
4、关于Mysql的HASH分区技术的小案例
create table employees_4 ( id int ) <strong><span style="color:#ff0000;">partition by hash(id) partitions 4;</span></strong> insert into employees_4 values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); insert into employees_4 values(11),(12),(13),(14),(15),(16),(17),(18),(19),(20); -rw-rw---- 1 mysql mysql 8556 Apr 7 04:22 employees_4.frm -rw-rw---- 1 mysql mysql 32 Apr 7 04:22 employees_4.par -rw-rw---- 1 mysql mysql 98304 Apr 7 04:22 employees_4#P#p0.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:22 employees_4#P#p1.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:22 employees_4#P#p2.ibd -rw-rw---- 1 mysql mysql 98304 Apr 7 04:22 employees_4#P#p3.ibd