<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog]]></title> 
<link>http://www.bizeway.net/index.php</link> 
<description><![CDATA[网务通 - 网务公司发展之路]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog]]></copyright>
<item>
<link>http://www.bizeway.net/read.php?</link>
<title><![CDATA[mysql分区功能详细介绍以及实例]]></title> 
<author>ArthurXF &lt;arthurxf@gmail.com&gt;</author>
<category><![CDATA[MySQL]]></category>
<pubDate>Sat, 27 Jul 2013 09:39:30 +0000</pubDate> 
<guid>http://www.bizeway.net/read.php?</guid> 
<description>
<![CDATA[ 
	一，什么是数据库分区<br/>前段时间写过一篇关于mysql分表的的文章，下面来说一下什么是数据库分区，以mysql为例。mysql数据库中的数据是以文件的形势存在磁盘上的，默认放在/mysql/data下面（可以通过my.cnf中的datadir来查看），一张表主要对应着三个文件，一个是frm存放表结构的，一个是myd存放表数据的，一个是myi存表索引的。如果一张表的数据量太大的话，那么myd,myi就会变的很大，查找数据就会变的很慢，这个时候我们可以利用mysql的分区功能，在物理上将这一张表对应的三个文件，分割成许多个小块，这样呢，我们查找一条数据时，就不用全部查找了，只要知道这条数据在哪一块，然后在那一块找就行了。如果表的数据太大，可能一个磁盘放不下，这个时候，我们可以把数据分配到不同的磁盘里面去。<br/><br/>备注说明：上面是只对myisam存储引擎的，下面是innodb<br/>innodb的数据库的物理文件结构为：<br/><br/>.frm文件<br/><br/>.ibd文件和.ibdata文件：<br/><br/>这两种文件都是存放innodb数据的文件，之所以用两种文件来存放innodb的数据，是因为innodb的数据存储方式能够通过配置来决定是使用共享表空间存放存储数据，还是用独享表空间存放存储数据。<br/><br/>独享表空间存储方式使用.ibd文件，并且每个表一个ibd文件<br/><br/>共享表空间存储方式使用.ibdata文件，所有表共同使用一个ibdata文件<br/><br/><br/>分区的二种方式<br/>1，横向分区<br/>什么是横向分区呢？就是横着来分区了，举例来说明一下，假如有100W条数据，分成十份，前10W条数据放到第一个分区，第二个10W条数据放到第二个分区，依此类推。也就是把表分成了十分，根用merge来分表，有点像哦。取出一条数据的时候，这条数据包含了表结构中的所有字段，也就是说横向分区，并没有改变表的结构。<br/><br/>2，纵向分区<br/>什么是纵向分区呢？就是竖来分区了，举例来说明，在设计用户表的时候，开始的时候没有考虑好，而把个人的所有信息都放到了一张表里面去，这样这个表里面就会有比较大的字段，如个人简介，而这些简介呢，也许不会有好多人去看，所以等到有人要看的时候，在去查找，分表的时候，可以把这样的大字段，分开来。<br/><br/>感觉数据库的分区好像是切苹果，到底是横着切呢，还是竖着切，根据个人喜好了，mysql提供的分区属于第一种，横向分区，并且细分成很多种方式。下面将举例说明一下。<br/><br/>二，mysql的分区<br/>我觉着吧，mysql的分区只有一种方式，只不过运用不同的算法，規则将数据分配到不同的区块中而已。<br/><br/>1，mysql5.1及以上支持分区功能<br/>安装安装的时候，我们就可以查看一下<br/><br/>view sourceprint?1 [root@BlackGhost mysql-5.1.50]# ./configure --help &#124;grep -A 3 Partition &nbsp;<br/><br/>2 &nbsp;=== Partition Support === &nbsp;<br/><br/>3 &nbsp;Plugin Name: &nbsp; &nbsp; &nbsp;partition &nbsp;<br/><br/>4 &nbsp;Description: &nbsp; &nbsp; &nbsp;MySQL Partitioning Support &nbsp;<br/><br/>5 &nbsp;Supports build: &nbsp; static <br/><br/>6 &nbsp;Configurations: &nbsp; max, max-no-ndb <br/><br/>查看一下，如果发现有上面这个东西，说明他是支持分区的，默认是打开的。如果你已经安装过了mysql的话<br/><br/>view sourceprint?1 mysql> show variables like "%part%"; &nbsp;<br/><br/>2 +-------------------+-------+ &nbsp;<br/><br/>3 &#124; Variable_name &nbsp; &nbsp; &#124; Value &#124; &nbsp;<br/><br/>4 +-------------------+-------+ &nbsp;<br/><br/>5 &#124; have_partitioning &#124; YES &nbsp; &#124; &nbsp;<br/><br/>6 +-------------------+-------+ &nbsp;<br/><br/>7 1 row in set (0.00 sec) <br/><br/>查看一下变量，如果支持的话，会有上面的提示的。<br/><br/>2，range分区<br/>按照RANGE分区的表是通过如下一种方式进行分区的，每个分区包含那些分区表达式的值位于一个给定的连续区间内的行<br/><br/>view sourceprint?1 //创建range分区表 &nbsp;<br/><br/>2 mysql> CREATE TABLE IF NOT EXISTS `user` ( &nbsp;<br/><br/>3 &nbsp;-> &nbsp; `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID', &nbsp;<br/><br/>4 &nbsp;-> &nbsp; `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名称', &nbsp;<br/><br/>5 &nbsp;-> &nbsp; `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0为男，1为女', &nbsp;<br/><br/>6 &nbsp;-> &nbsp; PRIMARY KEY (`id`) &nbsp;<br/><br/>7 &nbsp;-> ) ENGINE=MyISAM &nbsp;DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 &nbsp;<br/><br/>8 &nbsp;-> PARTITION BY RANGE (id) ( &nbsp;<br/><br/>9 &nbsp;-> &nbsp; &nbsp; PARTITION p0 VALUES LESS THAN (3), &nbsp;<br/><br/>10 &nbsp;-> &nbsp; &nbsp; PARTITION p1 VALUES LESS THAN (6), &nbsp;<br/><br/>11 &nbsp;-> &nbsp; &nbsp; PARTITION p2 VALUES LESS THAN (9), &nbsp;<br/><br/>12 &nbsp;-> &nbsp; &nbsp; PARTITION p3 VALUES LESS THAN (12), &nbsp;<br/><br/>13 &nbsp;-> &nbsp; &nbsp; PARTITION p4 VALUES LESS THAN MAXVALUE &nbsp;<br/><br/>14 &nbsp;-> ); &nbsp;<br/><br/>15 Query OK, 0 rows affected (0.13 sec) &nbsp;<br/><br/>16 &nbsp; &nbsp;<br/><br/>17 //插入一些数据 &nbsp;<br/><br/>18 mysql> INSERT INTO `test`.`user` (`name` ,`sex`)VALUES ('tank', '0') &nbsp;<br/><br/>19 &nbsp;-> ,('zhang',1),('ying',1),('张',1),('映',0),('test1',1),('tank2',1) &nbsp;<br/><br/>20 &nbsp;-> ,('tank1',1),('test2',1),('test3',1),('test4',1),('test5',1),('tank3',1) &nbsp;<br/><br/>21 &nbsp;-> ,('tank4',1),('tank5',1),('tank6',1),('tank7',1),('tank8',1),('tank9',1) &nbsp;<br/><br/>22 &nbsp;-> ,('tank10',1),('tank11',1),('tank12',1),('tank13',1),('tank21',1),('tank42',1); &nbsp;<br/><br/>23 Query OK, 25 rows affected (0.05 sec) &nbsp;<br/><br/>24 Records: 25 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>25 &nbsp; &nbsp;<br/><br/>26 //到存放数据库表文件的地方看一下，my.cnf里面有配置，datadir后面就是 &nbsp;<br/><br/>27 [root@BlackGhost test]# ls &#124;grep user &#124;xargs du -sh &nbsp;<br/><br/>28 4.0K &nbsp; &nbsp;user#P#p0.MYD &nbsp;<br/><br/>29 4.0K &nbsp; &nbsp;user#P#p0.MYI &nbsp;<br/><br/>30 4.0K &nbsp; &nbsp;user#P#p1.MYD &nbsp;<br/><br/>31 4.0K &nbsp; &nbsp;user#P#p1.MYI &nbsp;<br/><br/>32 4.0K &nbsp; &nbsp;user#P#p2.MYD &nbsp;<br/><br/>33 4.0K &nbsp; &nbsp;user#P#p2.MYI &nbsp;<br/><br/>34 4.0K &nbsp; &nbsp;user#P#p3.MYD &nbsp;<br/><br/>35 4.0K &nbsp; &nbsp;user#P#p3.MYI &nbsp;<br/><br/>36 4.0K &nbsp; &nbsp;user#P#p4.MYD &nbsp;<br/><br/>37 4.0K &nbsp; &nbsp;user#P#p4.MYI &nbsp;<br/><br/>38 12K &nbsp; &nbsp;user.frm &nbsp;<br/><br/>39 4.0K &nbsp; &nbsp;user.par &nbsp;<br/><br/>40 &nbsp; &nbsp;<br/><br/>41 //取出数据 &nbsp;<br/><br/>42 mysql> select count(id) as count from user; &nbsp;<br/><br/>43 +-------+ &nbsp;<br/><br/>44 &#124; count &#124; &nbsp;<br/><br/>45 +-------+ &nbsp;<br/><br/>46 &#124; &nbsp; &nbsp;25 &#124; &nbsp;<br/><br/>47 +-------+ &nbsp;<br/><br/>48 1 row in set (0.00 sec) &nbsp;<br/><br/>49 &nbsp; &nbsp;<br/><br/>50 //删除第四个分区 &nbsp;<br/><br/>51 mysql> alter table user drop partition p4; &nbsp;<br/><br/>52 Query OK, 0 rows affected (0.11 sec) &nbsp;<br/><br/>53 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>54 &nbsp; &nbsp;<br/><br/>55 /**存放在分区里面的数据丢失了，第四个分区里面有14条数据，剩下的3个分区 &nbsp;<br/><br/>56 只有11条数据，但是统计出来的文件大小都是4.0K，从这儿我们可以看出分区的 &nbsp;<br/><br/>57 最小区块是4K &nbsp;<br/><br/>58 */ <br/><br/>59 mysql> select count(id) as count from user; &nbsp;<br/><br/>60 +-------+ &nbsp;<br/><br/>61 &#124; count &#124; &nbsp;<br/><br/>62 +-------+ &nbsp;<br/><br/>63 &#124; &nbsp; &nbsp;11 &#124; &nbsp;<br/><br/>64 +-------+ &nbsp;<br/><br/>65 1 row in set (0.00 sec) &nbsp;<br/><br/>66 &nbsp; &nbsp;<br/><br/>67 //第四个区块已删除 &nbsp;<br/><br/>68 [root@BlackGhost test]# ls &#124;grep user &#124;xargs du -sh &nbsp;<br/><br/>69 4.0K &nbsp; &nbsp;user#P#p0.MYD &nbsp;<br/><br/>70 4.0K &nbsp; &nbsp;user#P#p0.MYI &nbsp;<br/><br/>71 4.0K &nbsp; &nbsp;user#P#p1.MYD &nbsp;<br/><br/>72 4.0K &nbsp; &nbsp;user#P#p1.MYI &nbsp;<br/><br/>73 4.0K &nbsp; &nbsp;user#P#p2.MYD &nbsp;<br/><br/>74 4.0K &nbsp; &nbsp;user#P#p2.MYI &nbsp;<br/><br/>75 4.0K &nbsp; &nbsp;user#P#p3.MYD &nbsp;<br/><br/>76 4.0K &nbsp; &nbsp;user#P#p3.MYI &nbsp;<br/><br/>77 12K &nbsp; &nbsp;user.frm &nbsp;<br/><br/>78 4.0K &nbsp; &nbsp;user.par &nbsp;<br/><br/>79 &nbsp; &nbsp;<br/><br/>80 /*可以对现有表进行分区,并且会按規则自动的将表中的数据分配相应的分区 &nbsp;<br/><br/>81 中，这样就比较好了，可以省去很多事情，看下面的操作*/ <br/><br/>82 mysql> alter table aa partition by RANGE(id) &nbsp;<br/><br/>83 &nbsp;-> (PARTITION p1 VALUES less than (1), &nbsp;<br/><br/>84 &nbsp;-> PARTITION p2 VALUES less than (5), &nbsp;<br/><br/>85 &nbsp;-> PARTITION p3 VALUES less than MAXVALUE); &nbsp;<br/><br/>86 Query OK, 15 rows affected (0.21 sec) &nbsp; //对15数据进行分区 &nbsp;<br/><br/>87 Records: 15 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>88 &nbsp; &nbsp;<br/><br/>89 //总共有15条 &nbsp;<br/><br/>90 mysql> select count(*) from aa; &nbsp;<br/><br/>91 +----------+ &nbsp;<br/><br/>92 &#124; count(*) &#124; &nbsp;<br/><br/>93 +----------+ &nbsp;<br/><br/>94 &#124; &nbsp; &nbsp; &nbsp; 15 &#124; &nbsp;<br/><br/>95 +----------+ &nbsp;<br/><br/>96 1 row in set (0.00 sec) &nbsp;<br/><br/>97 &nbsp; &nbsp;<br/><br/>98 //删除一个分区 &nbsp;<br/><br/>99 mysql> alter table aa drop partition p2; &nbsp;<br/><br/>100 Query OK, 0 rows affected (0.30 sec) &nbsp;<br/><br/>101 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>102 &nbsp; &nbsp;<br/><br/>103 //只有11条了，说明对现有的表分区成功了 &nbsp;<br/><br/>104 mysql> select count(*) from aa; &nbsp;<br/><br/>105 +----------+ &nbsp;<br/><br/>106 &#124; count(*) &#124; &nbsp;<br/><br/>107 +----------+ &nbsp;<br/><br/>108 &#124; &nbsp; &nbsp; &nbsp; 11 &#124; &nbsp;<br/><br/>109 +----------+ &nbsp;<br/><br/>110 1 row in set (0.00 sec) <br/><br/>3，list分区<br/>LIST分区中每个分区的定义和选择是基于某列的值从属于一个值列表集中的一个值，而RANGE分 区是从属于一个连续区间值的集合。<br/><br/> <br/><br/>view sourceprint?1 //这种方式失败 &nbsp;<br/><br/>2 mysql> CREATE TABLE IF NOT EXISTS `list_part` ( &nbsp;<br/><br/>3 &nbsp;-> &nbsp; `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID', &nbsp;<br/><br/>4 &nbsp;-> &nbsp; `province_id` int(2) NOT NULL DEFAULT 0 COMMENT '省', &nbsp;<br/><br/>5 &nbsp;-> &nbsp; `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名称', &nbsp;<br/><br/>6 &nbsp;-> &nbsp; `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0为男，1为女', &nbsp;<br/><br/>7 &nbsp;-> &nbsp; PRIMARY KEY (`id`) &nbsp;<br/><br/>8 &nbsp;-> ) ENGINE=INNODB &nbsp;DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 &nbsp;<br/><br/>9 &nbsp;-> PARTITION BY LIST (province_id) ( &nbsp;<br/><br/>10 &nbsp;-> &nbsp; &nbsp; PARTITION p0 VALUES IN (1,2,3,4,5,6,7,8), &nbsp;<br/><br/>11 &nbsp;-> &nbsp; &nbsp; PARTITION p1 VALUES IN (9,10,11,12,16,21), &nbsp;<br/><br/>12 &nbsp;-> &nbsp; &nbsp; PARTITION p2 VALUES IN (13,14,15,19), &nbsp;<br/><br/>13 &nbsp;-> &nbsp; &nbsp; PARTITION p3 VALUES IN (17,18,20,22,23,24) &nbsp;<br/><br/>14 &nbsp;-> ); &nbsp;<br/><br/>15 ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function <br/><br/>16 &nbsp; &nbsp;<br/><br/>17 //这种方式成功 &nbsp;<br/><br/>18 mysql> CREATE TABLE IF NOT EXISTS `list_part` ( &nbsp;<br/><br/>19 &nbsp;-> &nbsp; `id` int(11) NOT NULL &nbsp;COMMENT '用户ID', &nbsp;<br/><br/>20 &nbsp;-> &nbsp; `province_id` int(2) NOT NULL DEFAULT 0 COMMENT '省', &nbsp;<br/><br/>21 &nbsp;-> &nbsp; `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名称', &nbsp;<br/><br/>22 &nbsp;-> &nbsp; `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0为男，1为女' <br/><br/>23 &nbsp;-> ) ENGINE=INNODB &nbsp;DEFAULT CHARSET=utf8 &nbsp;<br/><br/>24 &nbsp;-> PARTITION BY LIST (province_id) ( &nbsp;<br/><br/>25 &nbsp;-> &nbsp; &nbsp; PARTITION p0 VALUES IN (1,2,3,4,5,6,7,8), &nbsp;<br/><br/>26 &nbsp;-> &nbsp; &nbsp; PARTITION p1 VALUES IN (9,10,11,12,16,21), &nbsp;<br/><br/>27 &nbsp;-> &nbsp; &nbsp; PARTITION p2 VALUES IN (13,14,15,19), &nbsp;<br/><br/>28 &nbsp;-> &nbsp; &nbsp; PARTITION p3 VALUES IN (17,18,20,22,23,24) &nbsp;<br/><br/>29 &nbsp;-> ); &nbsp;<br/><br/>30 Query OK, 0 rows affected (0.33 sec) <br/><br/> <br/><br/>上面的这个创建list分区时，如果有主銉的话，分区时主键必须在其中，不然就会报错。如果我不用主键，分区就创建成功了，一般情况下，一个张表肯定会有一个主键，这算是一个分区的局限性吧。<br/><br/>如果对数据进行测试，请参考range分区的测试来操作<br/><br/>4，hash分区<br/>HASH分区主要用来确保数据在预先确定数目的分区中平均分布，你所要做的只是基于将要被哈希的列值指定一个列值或表达式，以 及指定被分区的表将要被分割成的分区数量。<br/><br/>view sourceprint?1 mysql> CREATE TABLE IF NOT EXISTS `hash_part` ( &nbsp;<br/><br/>2 &nbsp;-> &nbsp; `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '评论ID', &nbsp;<br/><br/>3 &nbsp;-> &nbsp; `comment` varchar(1000) NOT NULL DEFAULT '' COMMENT '评论', &nbsp;<br/><br/>4 &nbsp;-> &nbsp; `ip` varchar(25) NOT NULL DEFAULT '' COMMENT '来源IP', &nbsp;<br/><br/>5 &nbsp;-> &nbsp; PRIMARY KEY (`id`) &nbsp;<br/><br/>6 &nbsp;-> ) ENGINE=INNODB &nbsp;DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 &nbsp;<br/><br/>7 &nbsp;-> PARTITION BY HASH(id) &nbsp;<br/><br/>8 &nbsp;-> PARTITIONS 3; &nbsp;<br/><br/>9 Query OK, 0 rows affected (0.06 sec) <br/><br/>测试请参考range分区的操作<br/><br/>5，key分区<br/>按照KEY进行分区类似于按照HASH分区，除了HASH分区使用的用 户定义的表达式，而KEY分区的 哈希函数是由MySQL 服务器提供。<br/><br/>view sourceprint?1 mysql> CREATE TABLE IF NOT EXISTS `key_part` ( &nbsp;<br/><br/>2 &nbsp;-> &nbsp; `news_id` int(11) NOT NULL &nbsp;COMMENT '新闻ID', &nbsp;<br/><br/>3 &nbsp;-> &nbsp; `content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新闻内容', &nbsp;<br/><br/>4 &nbsp;-> &nbsp; `u_id` varchar(25) NOT NULL DEFAULT '' COMMENT '来源IP', &nbsp;<br/><br/>5 &nbsp;-> &nbsp; `create_time` DATE NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '时间' <br/><br/>6 &nbsp;-> ) ENGINE=INNODB &nbsp;DEFAULT CHARSET=utf8 &nbsp;<br/><br/>7 &nbsp;-> PARTITION BY LINEAR HASH(YEAR(create_time)) &nbsp;<br/><br/>8 &nbsp;-> PARTITIONS 3; &nbsp;<br/><br/>9 Query OK, 0 rows affected (0.07 sec) <br/><br/>测试请参考range分区的操作<br/><br/>6，子分区<br/>子分区是分区表中每个分区的再次分割，子分区既可以使用HASH希分区，也可以使用KEY分区。这 也被称为复合分区（composite partitioning）。<br/><br/>1，如果一个分区中创建了子分区，其他分区也要有子分区<br/><br/>2，如果创建了了分区，每个分区中的子分区数必有相同<br/><br/>3，同一分区内的子分区，名字不相同，不同分区内的子分区名子可以相同（5.1.50不适用）<br/><br/>view sourceprint?1 mysql> CREATE TABLE IF NOT EXISTS `sub_part` ( &nbsp;<br/><br/>2 &nbsp;-> &nbsp; `news_id` int(11) NOT NULL &nbsp;COMMENT '新闻ID', &nbsp;<br/><br/>3 &nbsp;-> &nbsp; `content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新闻内容', &nbsp;<br/><br/>4 &nbsp;-> &nbsp; `u_id` &nbsp;int(11) NOT NULL DEFAULT 0s COMMENT '来源IP', &nbsp;<br/><br/>5 &nbsp;-> &nbsp; `create_time` DATE NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '时间' <br/><br/>6 &nbsp;-> ) ENGINE=INNODB &nbsp;DEFAULT CHARSET=utf8 &nbsp;<br/><br/>7 &nbsp;-> PARTITION BY RANGE(YEAR(create_time)) &nbsp;<br/><br/>8 &nbsp;-> SUBPARTITION BY HASH(TO_DAYS(create_time))( &nbsp;<br/><br/>9 &nbsp;-> PARTITION p0 VALUES LESS THAN (1990)(SUBPARTITION s0,SUBPARTITION s1,SUBPARTITION s2), &nbsp;<br/><br/>10 &nbsp;-> PARTITION p1 VALUES LESS THAN (2000)(SUBPARTITION s3,SUBPARTITION s4,SUBPARTITION good), &nbsp;<br/><br/>11 &nbsp;-> PARTITION p2 VALUES LESS THAN MAXVALUE(SUBPARTITION tank0,SUBPARTITION tank1,SUBPARTITION tank3) &nbsp;<br/><br/>12 &nbsp;-> ); &nbsp;<br/><br/>13 Query OK, 0 rows affected (0.07 sec) <br/><br/>三，分区管理<br/>1，删除分区<br/><br/>mysql> alter table user drop partition p4; &nbsp;<br/>2，新增分区<br/><br/>view sourceprint?1 //range添加新分区 &nbsp;<br/><br/>2 mysql> alter table user add partition(partition p4 values less than MAXVALUE); &nbsp;<br/><br/>3 Query OK, 0 rows affected (0.06 sec) &nbsp;<br/><br/>4 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>5 &nbsp; &nbsp;<br/><br/>6 //list添加新分区 &nbsp;<br/><br/>7 mysql> alter table list_part add partition(partition p4 values in (25,26,28)); &nbsp;<br/><br/>8 Query OK, 0 rows affected (0.01 sec) &nbsp;<br/><br/>9 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>10 &nbsp; &nbsp;<br/><br/>11 //hash重新分区 &nbsp;<br/><br/>12 mysql> alter table hash_part add partition partitions 4; &nbsp;<br/><br/>13 Query OK, 0 rows affected (0.12 sec) &nbsp;<br/><br/>14 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>15 &nbsp; &nbsp;<br/><br/>16 //key重新分区 &nbsp;<br/><br/>17 mysql> alter table key_part add partition partitions 4; &nbsp;<br/><br/>18 Query OK, 1 row affected (0.06 sec) &nbsp; &nbsp;//有数据也会被重新分配 &nbsp;<br/><br/>19 Records: 1 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>20 &nbsp; &nbsp;<br/><br/>21 //子分区添加新分区，虽然我没有指定子分区，但是系统会给子分区命名的 &nbsp;<br/><br/>22 mysql> alter table sub1_part add partition(partition p3 values less than MAXVALUE); &nbsp;<br/><br/>23 Query OK, 0 rows affected (0.02 sec) &nbsp;<br/><br/>24 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>25 &nbsp; &nbsp;<br/><br/>26 mysql> show create table sub1_partG; &nbsp;<br/><br/>27 *************************** 1. row *************************** &nbsp;<br/><br/>28 &nbsp;Table: sub1_part &nbsp;<br/><br/>29 Create Table: CREATE TABLE `sub1_part` ( &nbsp;<br/><br/>30 &nbsp;`news_id` int(11) NOT NULL COMMENT '新闻ID', &nbsp;<br/><br/>31 &nbsp;`content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新闻内容', &nbsp;<br/><br/>32 &nbsp;`u_id` varchar(25) NOT NULL DEFAULT '' COMMENT '来源IP', &nbsp;<br/><br/>33 &nbsp;`create_time` date NOT NULL DEFAULT '0000-00-00' COMMENT '时间' <br/><br/>34 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 &nbsp;<br/><br/>35 !50100 PARTITION BY RANGE (YEAR(create_time)) &nbsp;<br/><br/>36 SUBPARTITION BY HASH (TO_DAYS(create_time)) &nbsp;<br/><br/>37 (PARTITION p0 VALUES LESS THAN (1990) &nbsp;<br/><br/>38 &nbsp;(SUBPARTITION s0 ENGINE = InnoDB, &nbsp;<br/><br/>39 &nbsp;SUBPARTITION s1 ENGINE = InnoDB, &nbsp;<br/><br/>40 &nbsp;SUBPARTITION s2 ENGINE = InnoDB), &nbsp;<br/><br/>41 &nbsp;PARTITION p1 VALUES LESS THAN (2000) &nbsp;<br/><br/>42 &nbsp;(SUBPARTITION s3 ENGINE = InnoDB, &nbsp;<br/><br/>43 &nbsp;SUBPARTITION s4 ENGINE = InnoDB, &nbsp;<br/><br/>44 &nbsp;SUBPARTITION good ENGINE = InnoDB), &nbsp;<br/><br/>45 &nbsp;PARTITION p2 VALUES LESS THAN (3000) &nbsp;<br/><br/>46 &nbsp;(SUBPARTITION tank0 ENGINE = InnoDB, &nbsp;<br/><br/>47 &nbsp;SUBPARTITION tank1 ENGINE = InnoDB, &nbsp;<br/><br/>48 &nbsp;SUBPARTITION tank3 ENGINE = InnoDB), &nbsp;<br/><br/>49 &nbsp;PARTITION p3 VALUES LESS THAN MAXVALUE &nbsp;<br/><br/>50 &nbsp;(SUBPARTITION p3sp0 ENGINE = InnoDB, &nbsp; &nbsp;//子分区的名子是自动生成的 &nbsp;<br/><br/>51 &nbsp;SUBPARTITION p3sp1 ENGINE = InnoDB, &nbsp;<br/><br/>52 &nbsp;SUBPARTITION p3sp2 ENGINE = InnoDB)) &nbsp;<br/><br/>53 1 row in set (0.00 sec) <br/><br/><br/>3，重新分区<br/><br/>view sourceprint?1 //range重新分区 &nbsp;<br/><br/>2 mysql> ALTER TABLE user REORGANIZE PARTITION p0,p1,p2,p3,p4 INTO (PARTITION p0 VALUES LESS THAN MAXVALUE); &nbsp;<br/><br/>3 Query OK, 11 rows affected (0.08 sec) &nbsp;<br/><br/>4 Records: 11 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>5 &nbsp; &nbsp;<br/><br/>6 //list重新分区 &nbsp;<br/><br/>7 mysql> ALTER TABLE list_part REORGANIZE PARTITION p0,p1,p2,p3,p4 INTO (PARTITION p0 VALUES in (1,2,3,4,5)); &nbsp;<br/><br/>8 Query OK, 0 rows affected (0.28 sec) &nbsp;<br/><br/>9 Records: 0 &nbsp;Duplicates: 0 &nbsp;Warnings: 0 &nbsp;<br/><br/>10 &nbsp; &nbsp;<br/><br/>11 //hash和key分区不能用REORGANIZE，官方网站说的很清楚 &nbsp;<br/><br/>12 mysql> ALTER TABLE key_part REORGANIZE PARTITION COALESCE PARTITION 9; &nbsp;<br/><br/>13 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PARTITION 9' at line 1 <br/><br/>四，分区优点<br/>1，分区可以分在多个磁盘，存储更大一点<br/><br/>2，根据查找条件，也就是where后面的条件，查找只查找相应的分区不用全部查找了<br/><br/>3，进行大数据搜索时可以进行并行处理。<br/><br/>4，跨多个磁盘来分散数据查询，来获得更大的查询吞吐量<br/><br/>课外阅读<br/>：http://dev.mysql.com/doc/refman/5.1/zh/partitioning.html<br/><br/><br/>Tags - <a href="tag.php?tag=mysql" rel="tag">mysql</a> , <a href="tag.php?tag=%E5%88%86%E5%8C%BA" rel="tag">分区</a>
]]>
</description>
</item><item>
<link>http://www.bizeway.net/read.php?&amp;guid=0#topreply</link>
<title><![CDATA[[评论] mysql分区功能详细介绍以及实例]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.bizeway.net/read.php?&amp;guid=0#topreply</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>