您好,登錄后才能下訂單哦!
這篇“MySQL如何實現清空分區表單個分區數據”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“MySQL如何實現清空分區表單個分區數據”文章吧。
ALTER TABLE xxx TRUNCATE PARTITION p20220104;
功能:指定清空之前某一天的數據,直接調用存儲過程實現
DELIMITER $$ USE `managerdb`$$ DROP PROCEDURE IF EXISTS `partition_trunc`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `partition_trunc`(p_schema_name VARCHAR(64), p_table_name VARCHAR(64), p_trunc_before_date INT) BEGIN /* p_trunc_before_date 清空分區表第N天的數據 */ DECLARE trunc_part_name VARCHAR(16); SET trunc_part_name = CONCAT('p',DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL p_trunc_before_date DAY),'%Y%m%d')); SET @trunc_partitions = CONCAT("ALTER TABLE ", p_schema_name, ".", p_table_name, " TRUNCATE PARTITION ",trunc_part_name); -- 拼執行語句 SELECT @trunc_partitions; -- 打印刪除詳情 PREPARE STMT FROM @trunc_partitions; EXECUTE STMT; DEALLOCATE PREPARE STMT; END$$ DELIMITER ;
實例:
call managerdb.partition_trunc('test','t_001',1);
清空test.t_001一天前的單個分區數據
mysql分區表功能特別有用,其中一個應用就是保存固定時間的數據信息,自動分區自動purge,不用擔心數據量越積累越多。
比較實用的一個實現方式是表一天一個分區,保持固定天數的數據。
以數據庫log為例,里面有一個表tb_log, 按天分區,始終保存最新的30天的數據。
存儲過程sp_create_log_partition和sp_drop_log_partition用于創建和刪除分區。
事件event_log_auto_partition每天執行一次,用于向前創建新的分區和刪除過期的分區。
存儲過程和事件結合使用就實現了tb_log數據的自動分區自動刪除。
-- -- Definition for database log -- DROP DATABASE IF EXISTS log; CREATE DATABASE IF NOT EXISTS log CHARACTER SET utf8 COLLATE utf8_general_ci; -- -- Set default database -- USE log; -- -- Definition for table tb_log -- CREATE TABLE IF NOT EXISTS tb_log ( id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, log varchar(512) NOT NULL DEFAULT '', PRIMARY KEY (id, created_at) ) ENGINE = INNODB AUTO_INCREMENT = 1 AVG_ROW_LENGTH = 16384 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci PARTITION BY RANGE(TO_DAYS(created_at)) ( PARTITION pbasic VALUES LESS THAN (0) ); DELIMITER $$ -- -- Definition for procedure sp_create_log_partition -- CREATE DEFINER = 'uiadmin'@'%' PROCEDURE sp_create_log_partition (day_value datetime, tb_name varchar(128)) BEGIN DECLARE par_name varchar(32); DECLARE par_value varchar(32); DECLARE _err int(1); DECLARE par_exist int(1); DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND SET _err = 1; START TRANSACTION; SET par_name = CONCAT('p', DATE_FORMAT(day_value, '%Y%m%d')); SELECT COUNT(1) INTO par_exist FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = 'log' AND TABLE_NAME = tb_name AND PARTITION_NAME = par_name; IF (par_exist = 0) THEN SET par_value = DATE_FORMAT(day_value, '%Y-%m-%d'); SET @alter_sql = CONCAT('alter table ', tb_name, ' add PARTITION (PARTITION ', par_name, ' VALUES LESS THAN (TO_DAYS("', par_value, '")+1))'); PREPARE stmt1 FROM @alter_sql; EXECUTE stmt1; END IF; END $$ -- -- Definition for procedure sp_drop_log_partition -- CREATE DEFINER = 'uiadmin'@'%' PROCEDURE sp_drop_log_partition (day_value datetime, tb_name varchar(128)) BEGIN DECLARE str_day varchar(64); DECLARE _err int(1); DECLARE done int DEFAULT 0; DECLARE par_name varchar(64); DECLARE cur_partition_name CURSOR FOR SELECT partition_name FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA = 'log' AND table_name = tb_name ORDER BY partition_ordinal_position; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND SET _err = 1; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; SET str_day = DATE_FORMAT(day_value, '%Y%m%d'); OPEN cur_partition_name; REPEAT FETCH cur_partition_name INTO par_name; IF (str_day > SUBSTRING(par_name, 2)) THEN SET @alter_sql = CONCAT('alter table ', tb_name, ' drop PARTITION ', par_name); PREPARE stmt1 FROM @alter_sql; EXECUTE stmt1; END IF; UNTIL done END REPEAT; CLOSE cur_partition_name; END $$ -- -- Definition for event event_log_auto_partition -- CREATE DEFINER = 'uiadmin'@'%' EVENT event_log_auto_partition ON SCHEDULE EVERY '1' DAY STARTS '1972-01-01 00:00:00' ON COMPLETION PRESERVE DO BEGIN CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL - 3 DAY), 'tb_log'); CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL - 2 DAY), 'tb_log'); CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL - 1 DAY), 'tb_log'); CALL sp_create_log_partition(NOW(), 'tb_log'); CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL 1 DAY), 'tb_log'); CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL 2 DAY), 'tb_log'); CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL 3 DAY), 'tb_log'); CALL sp_drop_log_partition(DATE_ADD(NOW(), INTERVAL - 30 DAY), 'tb_log'); END $$ -- -- Create partitions based on current time -- CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL - 3 DAY), 'tb_log')$$ CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL - 2 DAY), 'tb_log')$$ CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL - 1 DAY), 'tb_log')$$ CALL sp_create_log_partition(NOW(), 'tb_log')$$ CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL 1 DAY), 'tb_log')$$ CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL 2 DAY), 'tb_log')$$ CALL sp_create_log_partition(DATE_ADD(NOW(), INTERVAL 3 DAY), 'tb_log')$$ DELIMITER ;
select TABLE_SCHEMA, TABLE_NAME,PARTITION_NAME from INFORMATION_SCHEMA.PARTITIONS where TABLE_SCHEMA='log' and table_name='tb_log';
在磁盤上一個分區表現為一個文件,所以刪除操作會很快完成的。
以上就是關于“MySQL如何實現清空分區表單個分區數據”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。