初探: 很长时间没写博客了,这两天一直在学习mysql分区,总结下:
mysql支持水平分区,并不支持垂直分区;
水平分区:指将同一表中不同行的记录分配到不同的物理文件中;
垂直分区:指将同一表中不同列的记录分配到不同的物理文件中;
其中csv、fedorated、merge等引擎不支持分区,myisam、innodb、ndb等引擎支持分区
目的: 将一个表或索引分解为多个更小、更可管理的部分,从逻辑上讲,只有一个表或者索引,但是物理上这个表或者索引可能由数十个物理分区组成;没个分区都是独立的对象,可以独自处理,也可以作为一个更大对象的一部分进行处理(如果分区表很大,亦可以将分区分配到不同的磁盘上去);在执行查询的时候,优化器会根据分区定义过滤哪些没有我们需要数据的分区,这样查询就无须全表扫描所有分区,只查找包含需要数据的分区即可
适用场景: 1、表非常大以至于无法全部都放到内存,或者只在表的最后部分有热点数据,其他均为历史数据
2、分区表数据更容易维护(可独立对分区进行优化、检查、修复及批量删除大数据可以采用drop分区的形式等)
3、分区表的数据可以分布在不同的物理设备上,从而高效地利用多个硬件设备
4、分区表可以避免某些特殊的瓶颈(ps: innodb的单个索引的互斥访问、ext3文件系统的inode锁竞争等)
5、可以备份和恢复独立的分区,非常适用于大数据集的场景
分区表限制: 单表最多支持1024个分区 mysql5.1只能对数据表的整型列进行分区,或者数据列可以通过分区函数转化成整型列;mysql5.5的range list类型可以直接使用列进行分区 如果分区字段中有主键或唯一索引的列,那么所有的主键列和唯一索引列都必须包含进来 分区表无法使用外键约束 分区必须使用相同的engine 对于myisam分区表,不能在使用load index into cache操作 对于myisam分区表,使用时会打开更多的文件描述符(单个分区是一个独立的文件) 分区策略: 全量扫描数据,不需要任何索引:通过where条件大概定位哪个分区,必须将查询所需要扫描的分区个数限制在很小的数量 建立分区索引,分离热点:如将明显的热点数据分离到一个分区,使其尽量缓存到内存中,这样就能充分使用索引和缓存
注意:以上策略均以查询得到过滤,丢掉额外的分区,分区本身不产生额外的代价为准则】 分区表使用过程的坑坑: null值会使分区过滤无效:
分表的表达式的值可以是null,第一个分区为特殊分区存放null或者非法值
如: partition by range year(order_date)进行分区,那么order_date为null或者非法值,记录存放在第一个分区:
where order_date between ‘2014-01-01’ and ‘2014-01-31’查询时会检查两个分区:
第一个分区及1月份分区,避免第一分区数据过大时造成查询代价过高,可以使用:建立第一分区专门存放order_date为null和非法值记录
partition p_nulls values less than(0)
mysql5.5以后可以才用一下语法解决问题:
partition by range columns(order_date) 2.分区列和索引列不匹配
此种情况下查询无法进行分区过滤,分区失效除非查询中包含了可以过滤分区的条件
3.range类型分区随着分区数量增加会对mysql额外增加查询分区定义列表(符合条件行在哪个分区)的压力,尽量限制适当的分区数量;key和hash类型分区不存在此问题
4.重组分区或者类似alter语句可能会造成很大的开销
新建或者删除分区操作很快,重组分区或者类似alter语句操作会先创建一个临时的分区,将数据复制其中,然后在删除原分区
分区表类型: 1.range分区:行数据基于属于一个给定连续区间的列值被放入分区
mysql5.5开始支持range columns的分区(引入columns分区解决了mysql 5.5版本之前range分区和list分区只支持整数分区,从而导致需要额外的函数计算得到整数或者通过额外的转换表来转换为整数再分区的问题。columns分区可以细分为range columns分区和list columns分区,range columns分区和list columns分区都支持整数、日期时间、字符串三大数据类型)
2.list分区:类似于按range分区,区别在于list分区是基于列值匹配一个离散值集合中的某个值来进行选择。
mysql5.5开始支持range columns的分区
3.hash分区:根据用户自定义的表达式的返回值来进行分区,返回值不能为负数
4.key分区:根据mysqls数据库提供的哈希函数来进行分区
【注:无论创建何种类型的分区,如果表中存在主键或唯一索引时,分区列必须是唯一索引的一个组成部分】
分区相关查询: 查看当前数据库是否支持分区mysql> show variables like '%partition%';+---------------------------------------+-------+| variable_name | value |+---------------------------------------+-------+| have_partitioning | yes || innodb_adaptive_hash_index_partitions | 1 |+---------------------------------------+-------+2 rows in set查看创建分区表的create语句mysql>show create table operation_log;查看表是否为分区表(create_options)mysql>show table status(当前库所有表状态)mysql>show table status from lockrank like '%operation_log%';(lockrank库operation_log表状态)*************************** 1. row ***************************table: operation_logcreate table: create table `operation_log` ( `id` int(11) unsigned not null auto_increment, `cid` mediumint(7) unsigned not null, `accountid` mediumint(8) not null default '0' , `flag` tinyint(1) unsigned not null default '0', `addtime` int(11) unsigned not null, `device` tinyint(1) unsigned not null default '1' , primary key (`id`,`addtime`), key `idx_accountid_addtime` (`accountid`,`addtime`), key `idx_accountid_flag` (`accountid`,`flag`),) engine=innodb auto_increment=50951039 default charset=utf8 comment='操作记录'/*!50100 partition by range (addtime)(partition `2013-05` values less than (1370016000) engine = innodb, partition `2013-06` values less than (1372608000) engine = innodb, partition `2013-07` values less than (1375286400) engine = innodb, partition `2013-08` values less than (1377964800) engine = innodb, partition `2013-09` values less than (1380556800) engine = innodb, partition `2013-10` values less than (1383235200) engine = innodb, partition `2013-11` values less than (1385827200) engine = innodb, partition `2013-12` values less than (1388505600) engine = innodb, partition `2014-01` values less than (1391184000) engine = innodb, partition `2014-02` values less than (1393603200) engine = innodb, partition `2014-03` values less than (1396281600) engine = innodb, partition `2014-04` values less than (1398873600) engine = innodb, partition `2014-05` values less than (1401552000) engine = innodb, partition `2014-06` values less than (1404144000) engine = innodb, partition `2014-07` values less than (1406822400) engine = innodb, partition `2014-08` values less than (1409500800) engine = innodb, partition `2014-09` values less than maxvalue engine = innodb) */1 row in set (0.00 sec)查看select如何使用分区mysql> explain partitions select id,accountid,cid,flag from operation_log where addtime=1369362524 /g ; *************************** 1. row *************************** id: 1 select_type: simpletable: operation_log partitions: 2013-05 type: allpossible_keys: null key: null key_len: null ref: null rows: 4384356extra: using where1 row in set (0.00 sec)``分区表元数据统计表:information_schema.partitions查看分区表operation_log的分区信息mysql> select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='operation_log';+---------+---------+------------+------------+| part| expr| descr | table_rows |+---------+---------+------------+------------+| 2013-05 | addtime | 1370016000 | 5999642 || 2013-06 | addtime | 1372608000 | 4579263 || 2013-07 | addtime | 1375286400 | 3223772 || 2013-08 | addtime | 1377964800 | 1995058 || 2013-09 | addtime | 1380556800 | 2497406 || 2013-10 | addtime | 1383235200 | 4106974 || 2013-11 | addtime | 1385827200 | 6209559 || 2013-12 | addtime | 1388505600 | 6415349 || 2014-01 | addtime | 1391184000 | 3953594 || 2014-02 | addtime | 1393603200 | 0 || 2014-03 | addtime | 1396281600 | 0 || 2014-04 | addtime | 1398873600 | 0 || 2014-05 | addtime | 1401552000 | 0 || 2014-06 | addtime | 1404144000 | 0 || 2014-07 | addtime | 1406822400 | 0 || 2014-08 | addtime | 1409500800 | 0 || 2014-09 | addtime | maxvalue | 0 |+---------+---------+------------+------------+17 rows in set (1.48 sec)
创建分区操作 range分区:mysql> create table `operation_log` ( -> `id` int(11) unsigned not null auto_increment, -> `cid` mediumint(7) unsigned not null, -> `accountid` mediumint(8) not null default '0' , -> `flag` tinyint(1) unsigned not null default '0', -> `addtime` int(11) unsigned not null, -> `device` tinyint(1) unsigned not null default '1' , -> primary key (`id`,`addtime`), -> key `idx_accountid_addtime` (`accountid`,`addtime`), -> key `idx_accountid_flag` (`accountid`,`flag`), ->) engine=innodb auto_increment=50951039 default charset=utf8 comment='操作记录' ->/*!50100 partition by range (addtime) ->(partition `2013-05` values less than (1370016000) engine = innodb, -> partition `2013-06` values less than (1372608000) engine = innodb, -> partition `2013-07` values less than (1375286400) engine = innodb, -> partition `2013-08` values less than (1377964800) engine = innodb, -> partition `2013-09` values less than maxvalue engine = innodb) */;1 row in set (0.00 sec)( less than maxvalue考虑到可能的最大值)list分区//这种方式失败mysql> create table if not exists `list_part` ( -> `id` int(11) not null auto_increment comment '用户id', -> `province_id` int(2) not null default 0 comment '省', -> `name` varchar(50) not null default '' comment '名称', -> `sex` int(1) not null default '0' comment '0为男,1为女', -> primary key (`id`) -> ) engine=innodb default charset=utf8 auto_increment=1 -> partition by list (province_id) ( -> partition p0 values in (1,2,3,4,5,6,7,8), -> partition p1 values in (9,10,11,12,16,21), -> partition p2 values in (13,14,15,19), -> partition p3 values in (17,18,20,22,23,24) -> );error 1503 (hy000): a primary key must include all columns in the table's partitioning function//这种方式成功mysql> create table if not exists `list_part` ( -> `id` int(11) not null comment '用户id', -> `province_id` int(2) not null default 0 comment '省', -> `name` varchar(50) not null default '' comment '名称', -> `sex` int(1) not null default '0' comment '0为男,1为女' -> ) engine=innodb default charset=utf8 -> partition by list (province_id) ( -> partition p0 values in (1,2,3,4,5,6,7,8), -> partition p1 values in (9,10,11,12,16,21), -> partition p2 values in (13,14,15,19), -> partition p3 values in (17,18,20,22,23,24) -> );query ok, 0 rows affected (0.33 sec)上面的这个创建list分区时,如果有主銉的话,分区时主键必须在其中,不然就会报错。如果我不用主键,分区就创建成功了,一般情况下,一个张表肯定会有一个主键,这算是一个分区的局限性hash分区mysql> create table if not exists `hash_part` ( -> `id` int(11) not null auto_increment comment '评论id', -> `comment` varchar(1000) not null default '' comment '评论', -> `ip` varchar(25) not null default '' comment '来源ip', -> primary key (`id`) -> ) engine=innodb default charset=utf8 auto_increment=1 -> partition by hash(id) -> partitions 3;query ok, 0 rows affected (0.06 sec)key分区 mysql> create table if not exists `key_part` ( -> `news_id` int(11) not null comment '新闻id', -> `content` varchar(1000) not null default '' comment '新闻内容', -> `u_id` varchar(25) not null default '' comment '来源ip', -> `create_time` date not null default '0000-00-00 00:00:00' comment '时间' -> ) engine=innodb default charset=utf8 -> partition by linear hash(year(create_time)) -> partitions 3;query ok, 0 rows affected (0.07 sec)
增加子分区操作:
子分区是分区表中每个分区的再次分割,子分区既可以使用hash希分区,也可以使用key分区。这 也被称为复合分区(composite partitioning)。
1. 如果一个分区中创建了子分区,其他分区也要有子分区2. 如果创建了了分区,每个分区中的子分区数必有相同3. 同一分区内的子分区,名字不相同,不同分区内的子分区名子可以相同(5.1.50不适用) mysql> create table if not exists `sub_part` ( -> `news_id` int(11) not null comment '新闻id', -> `content` varchar(1000) not null default '' comment '新闻内容', -> `u_id` int(11) not null default 0s comment '来源ip', -> `create_time` date not null default '0000-00-00 00:00:00' comment '时间' -> ) engine=innodb default charset=utf8 -> partition by range(year(create_time)) -> subpartition by hash(to_days(create_time))( -> partition p0 values less than (1990)(subpartition s0,subpartition s1,subpartition s2), -> partition p1 values less than (2000)(subpartition s3,subpartition s4,subpartition good), -> partition p2 values less than maxvalue(subpartition tank0,subpartition tank1,subpartition tank3) -> );query ok, 0 rows affected (0.07 sec)
分区管理:
增加分区操作(针对设置maxvalue) range添加分区mysql>alter table operation_log add partition(partition `2013-10` values less than (1383235200)); --->适用于没有设置maxvalue的分区添加 error 1481 (hy000):maxvalue can only be used in last partition definitionmysql>alter table operation_log reorganize partition `2013-09` into (partition `2013-09` values less than (1380556800),partition `2013-10` values less than (1383235200),partition `2013-11` values less than maxvalue); list添加分区mysql> alter table list_part add partition(partition p4 values in (25,26,28));query ok, 0 rows affected (0.01 sec)records: 0 duplicates: 0 warnings: 0 hash重新分区mysql> alter table list_part add partition(partition p4 values in (25,26,28));query ok, 0 rows affected (0.01 sec)records: 0 duplicates: 0 warnings: 0 key重新分区mysql> alter table key_part add partition partitions 4;query ok, 1 row affected (0.06 sec)//有数据也会被重新分配records: 1 duplicates: 0 warnings: 0子分区添加新分区,虽然我没有指定子分区,但是系统会给子分区命名的mysql> alter table sub1_part add partition(partition p3 values less than maxvalue);query ok, 0 rows affected (0.02 sec)records: 0 duplicates: 0 warnings: 0
删除分区操作
alter table user drop partition `2013-05`;
分区表其他操作 重建分区(官方:与先drop所有记录然后reinsert是一样的效果;用于整理表碎片)alter table operation_log rebuild partition `2014-01`;重建多个分区alter table operation_log rebuild partition `2014-01`,`2014-02`;过程如下:pro优化分区(如果删除了一个分区的大量记录或者对一个分区的varchar blob text数据类型的字段做了许多更新,此时可以对分区进行优化以回收未使用的空间和整理分区数据文件)alter table operation_log optimize partition `2014-01`;优化的操作相当于check partition,analyze partition 和repair patition分析分区alter table operation_log analyze partition `2014-01`;修复分区alter table operation_log repair partition `2014-01`;检查分区alter table operation_log check partition `2014-01`;
注释:
mysqlcheck、myisamchk并不支持分区表,analyze,check,optimize,rebuild,repair,truncate不支持子分区操作 在mysql5.6中,可以使用清空一个分区数据:alter table operation_log truncate partition 2014-01; 清空该分区表所有分区数据:alter table operation_log truncate partition all; 参考文档:
http://blog.51yip.com/mysql/1013.html
https://dev.mysql.com/doc/refman/5.6/en/partitioning-maintenance.html
http://dev.mysql.com/doc/refman/5.6/en/index.html