code学习

MySQL之分区表常用管理命令

一、ANALYZE和CHECK PARTITION 分析和检查分区

ANALYZE分析分区:

root@testdb 18:37: [testdb]> ALTER TABLE t_test_task_result ANALYZE PARTITION p20220218,p20220219;
+------------------------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------------------+---------+----------+----------+
| testdb.t_test_task_result | analyze | status | OK |
+------------------------------------------+---------+----------+----------+
1 row in set (0.01 sec)
           

CHECK 检查分区是否存在错误:

root@testdb 18:37: [testdb]> ALTER TABLE t_test_task_result check PARTITION p20220218,p20220219;
+------------------------------------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------------------+-------+----------+----------+
| testdb.t_test_task_result | check | status | OK |
+------------------------------------------+-------+----------+----------+
1 row in set (0.03 sec)
           

二、REPAIR 修复分区

ALTER TABLE t_test_task_result repair PARTITION p20220218,p20220219;
root@testdb 18:40: [testdb]> ALTER TABLE t_test_task_result repair PARTITION p20220218,p20220219;
+------------------------------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------------------+--------+----------+----------+
| testdb.t_test_task_result | repair | status | OK |
+------------------------------------------+--------+----------+----------+
           

三、OPTIMIZE 分区

该命令主要是用于回收空闲空间和分区的碎片整理。对分区执行该命令,相当于依次对分区执行 CHECK PARTITION, ANALYZE PARTITION,REPAIR PARTITION命令。会锁表

root@testdb 18:42: [testdb]> ALTER TABLE t_test_task_result OPTIMIZE PARTITION p20220218,p20220219;
+------------------------------------------+----------+----------+---------------------------------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------------------------------+----------+----------+---------------------------------------------------------------------------------------------+
| testdb.t_test_task_result | optimize | note | Table does not support optimize on partitions. All partitions will be rebuilt and analyzed. |
| testdb.t_test_task_result | optimize | status | OK |
+------------------------------------------+----------+----------+---------------------------------------------------------------------------------------------+
2 rows in set (1 min 35.75 sec)
           

四、REBUILD分区

重建分区,它相当于先删除分区中的数据,然后重新插入。这个主要是用于分区的碎片整理。这个操作会操作锁表

ALTER TABLE t_test_task_result REBUILD PARTITION p20220218,p20220219;
root@testdb 18:47: [testdb]> ALTER TABLE t_test_task_result REBUILD PARTITION p20220218,p20220219;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

root@testdb 18:47: [testdb]> ALTER TABLE t_test_task_result REBUILD PARTITION p20220225,p20220226,p20220227,p20220228;
Query OK, 0 rows affected (2 min 9.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
           

五、新增和删除分区

添加分区测试: ADD PARTITION(新增分区)

ALTER TABLE t_test_task_result ADD PARTITION (PARTITION p20220401 VALUES LESS THAN (TO_DAYS('2022-04-01')));

root@testdb 14:57: [testdb]> ALTER TABLE t_test_task_result ADD PARTITION (PARTITION p20220401 VALUES LESS THAN (TO_DAYS('2022-04-01')));
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
           
ALTER TABLE t_test_task_result DROP PARTITION p20220218;
##提示删除分区是物理删除,操作非常快
root@testdb 14:58: [testdb]> ALTER TABLE t_test_task_result DROP PARTITION p20220218;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@testdb 15:02: [testdb]> ALTER TABLE t_test_task_result DROP PARTITION p20220219,p20220220;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0