Rangeååºè¡¨å»ºè¡¨è¯å¥å¦ä¸ï¼å ¶ä¸ååºé®å¿ é¡»åidææ主é®åå¯ä¸é®
CREATE TABLE `test1` (
`id` char(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT âèªå¢ä¸»é®(guid)â,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT âå建æ¶é´â,
`partition_key` int(8) NOT NULL COMMENT âååºé®(æ ¼å¼:yyyyMMdd)â,
PRIMARY KEY (`id`,`partition_key`),
UNIQUE KEY `id_UNIQUE` (`id`,`partition_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
PARTITION BY RANGE (partition_key)
(PARTITION p0 VALUES LESS THAN (20180619) ENGINE = InnoDB,
PARTITION p20180619 VALUES LESS THAN (20180620) ENGINE = InnoDB,
PARTITION p20180621 VALUES LESS THAN (20180622) ENGINE = InnoDB,
PARTITION p20180622 VALUES LESS THAN (20180623) ENGINE = InnoDB,
PARTITION p20180623 VALUES LESS THAN (20180624) ENGINE = InnoDB);
æ°å¢ååº
alter TABLE `test1` add PARTITION(
PARTITION p20180629 VALUES LESS THAN (20180630) ENGINE = InnoDB
);
å é¤ååº
alter table `test1` drop PARTITION p20180629;
Mysqlä¸è½èªå¨å建ååºï¼éè¦ä½¿ç¨mysql eventäºä»¶çæ¹å¼èªå¨å建ååº
1.å建ååºçåå¨è¿ç¨å¦ä¸(æ¯æ¬¡æ§è¡å æ ¡éªå½åååºæ¯å¦åå¨ï¼å¦æåå¨åä¸å¤çï¼ä¸åå¨åå建)ï¼
DELIMITER $$
#该表æå¨æ°æ®åºå称
USE `demo`$$
DROP PROCEDURE IF EXISTS `create_partition_by_day`$$
CREATE PROCEDURE `create_partition_by_day`(IN_SCHEMANAME VARCHAR(64), IN_TABLENAME VARCHAR(64))
BEGIN
#å½åæ¥æåå¨çååºç个æ°
DECLARE ROWS_CNT INT UNSIGNED;
#ç®åæ¥æï¼ä¸ºå½åæ¥æçåä¸å¤©
DECLARE TARGET_DATE TIMESTAMP;
#ååºçå称ï¼æ ¼å¼ä¸ºp20180620
DECLARE PARTITIONNAME VARCHAR(9);
#å½åååºå称çååºå¼ä¸éï¼å³ä¸º PARTITIONNAME + 1
DECLARE PARTITION_ADD_DAY VARCHAR(9);
SET TARGET_DATE = NOW() + INTERVAL 1 DAY;
SET PARTITIONNAME = DATE_FORMAT( TARGET_DATE, âp%Y%m%dâ );
SET TARGET_DATE = TARGET_DATE + INTERVAL 1 DAY;
SET PARTITION_ADD_DAY = DATE_FORMAT( TARGET_DATE, â%Y%m%dâ );
SELECT COUNT(*) INTO ROWS_CNT FROM information_schema.partitions
WHERE table_schema = IN_SCHEMANAME AND table_name = IN_TABLENAME AND partition_name = PARTITIONNAME;
IF ROWS_CNT = 0 THEN
SET @SQL = CONCAT( âALTER TABLE `â, IN_SCHEMANAME, â`.`â, IN_TABLENAME, â`â,
â ADD PARTITION (PARTITION â, PARTITIONNAME, " VALUES LESS THAN (",
PARTITION_ADD_DAY ,") ENGINE = InnoDB);" );
PREPARE STMT FROM @SQL;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
ELSE
SELECT CONCAT("partition `", PARTITIONNAME, "` for table `",IN_SCHEMANAME, ".", IN_TABLENAME, "` already exists") AS result;
END IF;
END$$
DELIMITER ;
2.æ°æ®åºå®æ¶ä»»å¡(æ¯å°æ¶æ§è¡ä¸æ¬¡)
DELIMITER $$
#该表æå¨çæ°æ®åºå称
USE `demo`$$
CREATE EVENT IF NOT EXISTS `daily_generate_partition`
ON SCHEDULE EVERY 1 hour #æ§è¡å¨æï¼è¿æ天ãæçç
STARTS â2018-06-20 00:00:00â
ON COMPLETION PRESERVE
ENABLE
COMMENT âCreating partitionsâ
DO BEGIN
#è°ç¨åæå建çåå¨è¿ç¨ï¼ç¬¬ä¸ä¸ªåæ°æ¯æ°æ®åºå称ï¼ç¬¬äºä¸ªåæ°æ¯è¡¨å称
CALL datacollectcenter.create_partition_by_day(âdemoâ,âtest1â);
END$$
DELIMITER ;