以下是我对MySQL分区不分的理解,仅供参考。
MySQL从5.1版本后支持分区的功能,分区是根据一定规则,将满足相同条件的数据存储在一起,以便更方便的对数据经行管理。
在建立分区前,我们首先查看使用的MySQL版本是否支持分区:
show variables like '%partition%';
若是看到partition的值为yes,则支持分区。
分区类型:
- 1. range分区
- 2. list分区
- 3. hash分区
- 4. key分区
所有分区类型,不能使用主键/唯一键之外的字段经行分区!!!
1、range分区:依据给定的连续范围,将数据按照规则存储到对应分区上。
代码实现:
- range分区的创建:
mysql> create table ranges(
-> id int ,
-> age int,
-> time timestamp default current_timestamp on update current_timestamp,
-> primary key (id)
-> )
-> partition by range(id)(
-> partition p0 values less than (5),
-> partition p1 values less than (10),
-> partition p2 values less than (15)
-> );
Query OK, 0 rows affected (0.07 sec)
插入数据:
mysql> insert into ranges (id ,age) values(1,20),(2,21),(3,22),(4,23),(5,24),(6,29),(7,32),(8,43),(9,23),(10,54),(11,23),(12,45),(13,90),(14,54);
Query OK, 14 rows affected (0.00 sec)
Records: 14 Duplicates: 0 Warnings: 0
查询range分区相关信息:
mysql> select partition_name name,
-> partition_expression expresstion,
-> partition_description description,
-> table_rows
-> from information_schema.partitions
-> where table_schema=schema() and table_name='ranges';
+------+-------------+-------------+------------+
| name | expresstion | description | table_rows |
+------+-------------+-------------+------------+
| p0 | id | 5 | 4 |
| p1 | id | 10 | 5 |
| p2 | id | 15 | 5 |
+------+-------------+-------------+------------+
3 rows in set (0.00 sec)
当我们要插入大于id大于等于15的数时,会发生什么呢?
mysql> insert into ranges(id,age) values(15,30);
ERROR 1526 (HY000): Table has no partition for value 15
没有包含15的分区,这时我们应该添加range分区,指令实现如下:
- 添加分区
mysql> alter table ranges add partition (partition p3 values less than maxvalue);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
这一句是添加名为p3的分区,范围为[15,最大值),即大于等于15的所有值。
再次查看分区的情况:
mysql> select partition_name name,
-> partition_expression expresstion,
-> partition_description description,
-> table_rows
-> from information_schema.partitions
-> where table_schema=schema() and table_name='ranges';
+------+-------------+-------------+------------+
| name | expresstion | description | table_rows |
+------+-------------+-------------+------------+
| p0 | id | 5 | 4 |
| p1 | id | 10 | 5 |
| p2 | id | 15 | 5 |
| p3 | id | MAXVALUE | 0 | <----新加的分区
+------+-------------+-------------+------------+
4 rows in set (0.00 sec)
此时再次添加id大于15的值:
mysql> insert into ranges(id,age) values(19,20);
Query OK, 1 row affected (0.01 sec)
- 拆分分区
mysql> alter table ranges reorganize partition p3 into (
-> partition p4 values less than (20),
-> partition p5 values less than (25),
-> partition p6 values less than maxvalue
-> ) ;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
#查询拆分后的分区信息
mysql> select partition_name name,
-> partition_expression expresstion,
-> partition_description description,
-> table_rows
-> from information_schema.partitions
-> where table_schema=schema() and table_name='ranges';
+------+-------------+-------------+------------+
| name | expresstion | description | table_rows |
+------+-------------+-------------+------------+
| p0 | id | 5 | 4 |
| p1 | id | 10 | 5 |
| p2 | id | 15 | 5 |
| p4 | id | 20 | 0 |
| p5 | id | 25 | 0 |
| p6 | id | MAXVALUE | 0 |
+------+-------------+-------------+------------+
6 rows in set (0.00 sec)
这是将p3分区拆分为p4、p5、p6三个分区,这时就没有p3分区了。
- 合并分区:
mysql> alter table ranges reorganize partition p4,p5,p6 into(partition p3 values less than maxvalue);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
#查看合并后的分区信息
mysql> select partition_name name,
-> partition_expression expresstion,
-> partition_description description,
-> table_rows
-> from information_schema.partitions
-> where table_schema=schema() and table_name='ranges';
+------+-------------+-------------+------------+
| name | expresstion | description | table_rows |
+------+-------------+-------------+------------+
| p0 | id | 5 | 4 |
| p1 | id | 10 | 5 |
| p2 | id | 15 | 5 |
| p3 | id | MAXVALUE | 0 |
+------+-------------+-------------+------------+
4 rows in set (0.00 sec)
- 删除分区:
#删除前ranges表数据
mysql> select *from ranges;
+----+------+---------------------+
| id | age | time |
+----+------+---------------------+
| 1 | 20 | 2019-10-26 17:45:56 |
| 2 | 21 | 2019-10-26 17:45:56 |
| 3 | 22 | 2019-10-26 17:45:56 |
| 4 | 23 | 2019-10-26 17:45:56 |
| 5 | 24 | 2019-10-26 17:45:56 |
| 6 | 29 | 2019-10-26 17:45:56 |
| 7 | 32 | 2019-10-26 17:45:56 |
| 8 | 43 | 2019-10-26 17:45:56 |
| 9 | 23 | 2019-10-26 17:45:56 |
| 10 | 54 | 2019-10-26 17:45:56 |
| 11 | 23 | 2019-10-26 17:45:56 |
| 12 | 45 | 2019-10-26 17:45:56 |
| 13 | 90 | 2019-10-26 17:45:56 |
| 14 | 54 | 2019-10-26 17:45:56 |
| 15 | 20 | 2019-10-26 18:10:09 |
| 19 | 20 | 2019-10-26 18:10:18 |
+----+------+---------------------+
16 rows in set (0.01 sec)
#删除语句
mysql> alter table ranges drop partition p2 ;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
#删除后ranges表数据
mysql> select *from ranges;
+----+------+---------------------+
| id | age | time |
+----+------+---------------------+
| 1 | 20 | 2019-10-26 17:45:56 |
| 2 | 21 | 2019-10-26 17:45:56 |
| 3 | 22 | 2019-10-26 17:45:56 |
| 4 | 23 | 2019-10-26 17:45:56 |
| 5 | 24 | 2019-10-26 17:45:56 |
| 6 | 29 | 2019-10-26 17:45:56 |
| 7 | 32 | 2019-10-26 17:45:56 |
| 8 | 43 | 2019-10-26 17:45:56 |
| 9 | 23 | 2019-10-26 17:45:56 |
| 15 | 20 | 2019-10-26 18:10:09 |
| 19 | 20 | 2019-10-26 18:10:18 |
+----+------+---------------------+
11 rows in set (0.00 sec)
可见,删除某一分区后,该分区的数据也被删除。
range分区的大部分知识点已经完结,欢迎大家补充。谢谢
来源:https://blog.csdn.net/weixin_43576564/article/details/102758521