【MySQL】分区操作

本文介绍了如何在MySQL中创建分区表,包括使用`CREATETABLE`语句定义分区范围,以及如何执行分区查询、增加和删除分区的操作。
摘要由CSDN通过智能技术生成
  1. 创建分区表
CREATE TABLE `tb_table` (
  `record_id` varchar(255) NOT NULL,
  `insert_time` datetime NOT NULL,
  PRIMARY KEY (`record_id`, `insert_time`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE (TO_DAYS(insert_time)) (
		PARTITION p20231023 VALUES LESS THAN (TO_DAYS('2023-10-24')),
		PARTITION p20231024 VALUES LESS THAN (TO_DAYS('2023-10-25')),
		PARTITION p20231025 VALUES LESS THAN (TO_DAYS('2023-10-26')),
		PARTITION p20231026 VALUES LESS THAN (TO_DAYS('2023-10-27')),
		PARTITION p20231027 VALUES LESS THAN (TO_DAYS('2023-10-28')),
		PARTITION p20231028 VALUES LESS THAN (TO_DAYS('2023-10-29')),
		PARTITION p20231029 VALUES LESS THAN (TO_DAYS('2023-10-30')),
		PARTITION p20231030 VALUES LESS THAN (TO_DAYS('2023-10-31')),
		PARTITION p20231031 VALUES LESS THAN (TO_DAYS('2023-11-01')),
		PARTITION p20231101 VALUES LESS THAN (TO_DAYS('2023-11-02')),
		PARTITION p20231102 VALUES LESS THAN (TO_DAYS('2023-11-03')),
		PARTITION p20231103 VALUES LESS THAN (TO_DAYS('2023-11-04')),
		PARTITION p20231104 VALUES LESS THAN (TO_DAYS('2023-11-05')),
		PARTITION p20231105 VALUES LESS THAN (TO_DAYS('2023-11-06')),
		PARTITION p20231106 VALUES LESS THAN (TO_DAYS('2023-11-07')),
		PARTITION p20231107 VALUES LESS THAN (TO_DAYS('2023-11-08')),
		PARTITION p20231108 VALUES LESS THAN (TO_DAYS('2023-11-09')),
		PARTITION p20231109 VALUES LESS THAN (TO_DAYS('2023-11-10')),
		PARTITION p20231110 VALUES LESS THAN (TO_DAYS('2023-11-11')),
		PARTITION p20231111 VALUES LESS THAN (TO_DAYS('2023-11-12')),
		PARTITION p20231112 VALUES LESS THAN (TO_DAYS('2023-11-13')),
		PARTITION p20231113 VALUES LESS THAN (TO_DAYS('2023-11-14')),
		PARTITION p20231114 VALUES LESS THAN (TO_DAYS('2023-11-15')),
		PARTITION p20231115 VALUES LESS THAN (TO_DAYS('2023-11-16')),
		PARTITION p20231116 VALUES LESS THAN (TO_DAYS('2023-11-17')),
		PARTITION p20231117 VALUES LESS THAN (TO_DAYS('2023-11-18')),
		PARTITION p20231118 VALUES LESS THAN (TO_DAYS('2023-11-19')),
		PARTITION p20231119 VALUES LESS THAN (TO_DAYS('2023-11-20')),
		PARTITION p20231120 VALUES LESS THAN (TO_DAYS('2023-11-21')),
		PARTITION p20231121 VALUES LESS THAN (TO_DAYS('2023-11-22')),
		PARTITION p20231122 VALUES LESS THAN (TO_DAYS('2023-11-23')),
		PARTITION p20231123 VALUES LESS THAN (TO_DAYS('2023-11-24')),
		PARTITION p20231124 VALUES LESS THAN (TO_DAYS('2023-11-25')),
		PARTITION p20231125 VALUES LESS THAN (TO_DAYS('2023-11-26')),
		PARTITION p20231126 VALUES LESS THAN (TO_DAYS('2023-11-27')),
		PARTITION p20231127 VALUES LESS THAN (TO_DAYS('2023-11-28')),
		PARTITION p20231128 VALUES LESS THAN (TO_DAYS('2023-11-29'))
);
  1. 查询分区
SELECT * FROM information_schema.PARTITIONS WHERE TABLE_NAME = 'tb_table' and TABLE_SCHEMA = 'SCHEMA';
  1. 增加分区
ALTER TABLE tb_table add PARTITION (
    PARTITION p20240102 VALUES LESS THAN (TO_DAYS('2024-01-03'))
);
  1. 删除分区
ALTER TABLE tb_table drop PARTITION p20240103;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Brill_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值