前言
下面僅記錄一次對空表的一次分區過程,以日誌表爲例。
html
CREATE TABLE `sys_log` ( `log_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵ID', `user_id` int(10) DEFAULT NULL COMMENT '用戶ID', `class_function` varchar(255) DEFAULT NULL COMMENT '類和方法名', `function_id` int(10) DEFAULT NULL COMMENT '功能id', `description` varchar(255) DEFAULT NULL COMMENT '功能名稱', `sub_desc` varchar(255) DEFAULT NULL COMMENT '子功能', `ip` varchar(255) DEFAULT NULL, `create_time` datetime DEFAULT NULL COMMENT '建立時間', `type` int(10) DEFAULT NULL COMMENT '類型【1:異常 2:使用方法】', PRIMARY KEY (`log_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
建立聯合主鍵
刪除原有主鍵,建立聯合主鍵,這一步你能夠在建表的時候就建立好。
sql
ALTER TABLE `sys_log` DROP PRIMARY KEY,ADD PRIMARY KEY (log_id, create_time);
手動建立分區
手動建立分區,這一步你也能夠在建表的時候就建立好。
express
ALTER TABLE `sys_log` PARTITION by RANGE COLUMNS (create_time) (PARTITION `p1` VALUES LESS THAN ('2019-01-01'), PARTITION `p2` VALUES LESS THAN (MAXVALUE) ENGINE = InnoDB);
建立存儲過程
1.建立用於對單張表分區的存儲過程
less
CREATE DEFINER=`root`@`%` PROCEDURE `proc_create_partition`(in_tbname VARCHAR(64)) BEGIN SELECT DATABASE() INTO @dbname; SET @tbname = in_tbname; #查詢表的最近一次分區 SELECT REPLACE (partition_name, 'p', '') INTO @PMAX FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA = @dbname AND table_name = @tbname ORDER BY partition_ordinal_position DESC LIMIT 1; #查詢表的最近一次分區 SELECT REPLACE(partition_description, "'", '') INTO @DNAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA = @dbname AND table_name = @tbname ORDER BY partition_ordinal_position DESC LIMIT 1, 1; SET @t=CONCAT('alter table `',@dbname,'`.',@tbname,' reorganize partition p',@PMAX, ' into(partition p',@PMAX,' values less than (''',date(DATE_ADD(@DNAME,INTERVAL 1 MONTH)),'''),', 'partition p',@PMAX+1,' values less than MAXVALUE)'); SELECT @t; PREPARE stmt FROM @t; EXECUTE stmt; DEALLOCATE PREPARE stmt; COMMIT; END
2.建立存儲過程調用單表分區存儲過程對全部表進行分區
oop
CREATE DEFINER=`root`@`%` PROCEDURE `proc_create_partition_all`() BEGIN DECLARE tbname varchar(32); DECLARE tmpSql varchar(256); DECLARE done INT DEFAULT FALSE ; #查詢已手動分區的表 DECLARE part_cursor CURSOR FOR (SELECT DISTINCT table_name FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA = DATABASE() AND partition_expression IS NOT NULL AND table_name NOT LIKE '%bak'); DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; #循環對錶添加分區 OPEN part_cursor; myLoop: LOOP FETCH part_cursor INTO tbname; IF done THEN LEAVE myLoop; END IF; #調用分區存儲過程 CALL proc_create_partition(tbname); COMMIT; END LOOP myLoop; CLOSE part_cursor; END
建立事件
建立事件從2019年8月起每個月1號調用存儲過程給表添加分區
.net
CREATE DEFINER=`root`@`%` EVENT `e_create_partition` ON SCHEDULE EVERY 1 MONTH STARTS '2019-08-01 00:00:00' ON COMPLETION PRESERVE DISABLE DO CALL proc_create_partition_all()
參考:
https://blog.csdn.net/zhangkai19910815/article/details/96424294
https://blog.csdn.net/aofavx/article/details/50393281
https://www.cnblogs.com/freeton/p/4265228.html
https://blog.csdn.net/aofavx/article/details/50378360
————————————————
版權聲明:本文爲CSDN博主「洪城一笑」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/zhangkai19910815/article/details/96433467
日誌