看这篇文章前需要先了解一下以下几个问题~

一、问题

1.分区是什么

分区:就是把一张表数据分块存储

目的:提升索引的查询效率

2.mysql为什么要使用分区

先从数据分析

然后进行索引优化

然后引入分区

3.mysql中分区原理

客户端———> id 和分区键进行比较————->找到指定分区———->和数据库查询一致

4.mysql中分区局限

必须使用分区字段才行,不然分区查询就会失败。走所有分区。

目前range是范围分区,但是有时候我们会发现。分区大小永远是静态的。

所以会存在一个分区表大小不均。如何让分区表大小均衡呢?

二、分区落地实现

1.range分区

条件

  • product-partiton表

步骤

1、先创建product-partiton-range

create table `product-partiton-range` (
	`id` bigint(8) not null,
	`productname` char(245) not null default '1',
	`productid` char(255) not null default '1',
	`productdescription` char(255) not null default '1',
	`producturl` char(255) not null default '1',
	primary key (`id`),
	index `productid` (`productid`)
) engine=innodb default charset=utf8mb4
partition by range (id) partitions 3 (
partition part0 values less than (12980), 
partition part1 values less than (25960), 
partition part2 values less than maxvalue);

2、然后查询分区表

select * from product-partiton-range where id = 25000

2.hash分区

步骤

1、先创建product-partiton-hash

create table `product-partiton-hash` (
    `id` bigint(8) not null,
    `productname` char(245) not null default '1',
    `productid` char(255) not null default '1',
    `productdescription` char(255) not null default '1',
    `producturl` char(255) not null default '1',
    primary key (`id`),
    index `productid` (`productid`)
) engine=innodb default charset=utf8mb4
partition by hash (id) partitions 3;

hash分区只能进行数字字段进行分区,无法进行字符字段进行分区。如果需要对字段值进行分区。

必须包含在主键字段内。

3.key分区

步骤

1、先创建product-partiton-key

create table `product-partiton-key` (
	`id` bigint(8) not null,
	`productname` char(245) not null default '1',
	`productid` char(255) not null default '1',
	`productdescription` char(255) not null default '1',
	`producturl` char(255) not null default '1',
	primary key (`id`),
	index `productid` (`productid`)
) engine=innodb default charset=utf8mb4
partition by key (productname) partitions 3;

#建立复合主键
create table `product-partiton-key` (
	`id` bigint(8) not null,
	`productname` char(245) not null default '1',
	`productid` char(255) not null default '1',
	`productdescription` char(255) not null default '1',
	`producturl` char(255) not null default '1',
	primary key (`id`),
	index `productid` (`productid`)
) engine=innodb default charset=utf8mb4
partition by key (productname) partitions 3;

以上分区都是一个特点:所有的分区必须连续和连续大小进行分区。

我们再来看一个场景:如何对商品订单分区。

4.mysql中如何落地list分区

步骤

1、先创建product-partiton-list

create table `product-partiton-list` (
    `id` bigint(8) not null,
    `productname` char(245) not null default '1',
    `productid` char(255) not null default '1',
    `productdescription` char(255) not null default '1',
    `producturl` char(255) not null default '1',
    `productstatus` int not null default 0,
    primary key (`id`),
    index `productid` (`productid`)
) engine=innodb default charset=utf8mb4
partition by list(productid) (
    partition a values in (1,5,6),
    partition b values in (2,7,8)
);

商品主键和商品名称进行分区。

5.mysql中如何落地组合分区

步骤

create table `product-partiton-flex` (
    `id` bigint(8) not null,
    `productname` char(245) not null default '1',
    `productid` char(255) not null default '1',
    `productdescription` char(255) not null default '1',
    `producturl` char(255) not null default '1',
    primary key (`id`,`productname`),
    index `productid` (`productid`)
) engine=innodb default charset=utf8mb4
partition by range (id) partitions 3
subpartition by key(productname)
  subpartitions 2 (
    partition p0 values less than (12980),
    partition p1 values less than (25960),
    partition p2 values less than maxvalue
);

三、mysql如何管理分区

1.删除分区

alert table users drop partition p0; 
#删除分区 p0

2.重建分区

2.1range 分区重建

alter table users reorganize partition p0,p1 into (partition p0 values less than (6000000));  
#将原来的 p0,p1 分区合并起来,放到新的 p0 分区中。

2.2 list 分区重建

alter table users reorganize partition p0,p1 into (partition p0 values in(0,1,4,5,8,9,12,13));
#将原来的 p0,p1 分区合并起来,放到新的 p0 分区中。

2.3 hash/key 分区重建

alter table users reorganize partition coalesce partition 2; 
#用 reorganize 方式重建分区的数量变成2,在这里数量只能减少不能增加。想要增加可以用 add partition 方法。

3. 新增分区

3.1 新增 range 分区

#新增一个range分区
alter table category add partition (partition p4 values in (16,17,18,19)
            data directory = '/data8/data'
            index directory = '/data9/idx');

3.2 新增 hash/key 分区

alter table users add partition partitions 8;   #将分区总数扩展到8个。

3.3 给已有的表加上分区

alter table results partition by range (month(ttime)) 
(
partition p0 values less than (1),
partition p1 values less than (2) , 
partition p2 values less than (3) ,
partition p3 values less than (4) , 
partition p4 values less than (5) ,
partition p5 values less than (6) , 
partition p6 values less than (7) ,
partition p7 values less than (8) , 
partition p8 values less than (9) ,
partition p9 values less than (10) , 
partition p10 values less than (11),
partition p11 values less than (12),
partition p12 values less than (13) 
);

4.默认分区限制分区字段必须是主键(primary key)的一部分,去除此限制

[方法1] 使用id:

mysql> alter table np_pk
    ->     partition by hash( to_days(added) )
    ->     partitions 4;
#error 1503 (hy000): a primary key must include all columns in the table's partitioning function
mysql> alter table np_pk
    ->     partition by hash(id)
    ->     partitions 4;
query ok, 0 rows affected (0.11 sec)
records: 0 duplicates: 0 warnings: 0

[方法2] 将原有pk去掉生成新pk

mysql> alter table results drop primary key;
query ok, 5374850 rows affected (7 min 4.05 sec)
records: 5374850 duplicates: 0 warnings: 0

mysql> alter table results add primary key(id, ttime);
query ok, 5374850 rows affected (7 min 4.05 sec)
records: 5374850 duplicates: 0 warnings: 0

总结

到此这篇关于mysql四种分区方式以及组合分区落地实现的文章就介绍到这了,更多相关mysql组合分区落地内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!