当前位置:网站首页>MySQL creates partition tables and automatically partitions them by day
MySQL creates partition tables and automatically partitions them by day
2022-07-24 11:34:00 【Yabingshi】
# Create test database
create database csl_test character set utf8 collate utf8_unicode_ci;
# Create test table
notes : Fields to partition Primary key required .
use csl_test; # Switch to the test database just created
create table t_partition_test (
pk_id bigint(20) not null auto_increment,
time datetime not null,
msg varchar(200),
primary key (pk_id,time)
)engine=innodb default charset=utf8 collate utf8_unicode_ci comment="test partition";
# Insert test data
insert into t_partition_test(time,msg) values
('2021-02-01 13:34:23',"20210201133423"),
('2021-02-02 14:23:13',"20210202142313");
# Partition the table
# Modify the primary key field
alter table t_partition_test drop primary key, add primary key (pk_id, time);
# Batch partition
alter table t_partition_test partition by range columns(time)(
partition p20210201 values less than('2021-02-02'),
partition p20210202 values less than('2021-02-03'),
partition p20210203 values less than('2021-02-04')
);
# Look at the table partition
select partition_name,partition_description as val from information_schema.partitions
where table_name = 't_partition_test' and table_schema = 'csl_test';
# Create a stored procedure to add or delete partitions
-- Definition mysql Submit query statement End identification by $$
DELIMITER $$
DROP PROCEDURE IF EXISTS p_partition_test # The creation process p_partition_test
$$
CREATE PROCEDURE p_partition_test()
BEGIN
DECLARE v_sysdate DATE; # Statement current time
DECLARE v_mindate DATE; # Statement The minimum value of the current partition
DECLARE v_maxdate DATE; # Statement The maximum value in the current partition value
DECLARE v_pt VARCHAR(20); # Statement Partition name The digital part
DECLARE v_maxval VARCHAR(20); # Statement Maximum
DECLARE i INT; # Statement ??
-- Add new partition
-- select into A from B usage ;A Table as a temporary table , non-existent .
-- differ insert into A select from B,A There must be .
SELECT MAX(CAST(REPLACE(partition_description, '''', '') AS DATE)) AS val
INTO v_maxdate
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_partition_test' AND TABLE_SCHEMA = 'csl_test';
SET v_sysdate = SYSDATE(); # assignment v_sysdate For the current time
-- INTERVAL Keywords for time calculation
WHILE v_maxdate <= (v_sysdate + INTERVAL 3 DAY) DO
SET v_pt = DATE_FORMAT(v_maxdate ,'%Y%m%d');
SET v_maxval = DATE_FORMAT(v_maxdate + INTERVAL 1 DAY, '%Y-%m-%d');
SET @sql = CONCAT('alter table t_partition_test add partition (partition p', v_pt, ' values less than(''', v_maxval, '''))');
-- SQL Statement preprocessing , For repeated calls to the same or extremely similar statements , Preprocessing optimizes processing speed , I think the usage here should be ineffective
PREPARE stmt FROM @sql;
EXECUTE stmt;
-- Delete preprocessing
DEALLOCATE PREPARE stmt;
SET v_maxdate = v_maxdate + INTERVAL 1 DAY; # Maximum Add one operation
END WHILE;
-- Delete old partition
SELECT MIN(CAST(REPLACE(partition_description, '''', '') AS DATE)) AS val
INTO v_mindate
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't_partition_test' AND TABLE_SCHEMA = 'csl_test';
-- Delete the old partition one year ago
WHILE v_mindate <= (v_sysdate - INTERVAL 1 YEAR) DO
SET v_pt = DATE_FORMAT(v_mindate - INTERVAL 1 DAY,'%Y%m%d');
SET @sql = CONCAT('alter table t_partition_test drop partition p', v_pt);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET v_mindate = v_mindate + INTERVAL 1 DAY;
END WHILE;
END$$
-- Redefinition MySQL To submit a query statement End identification by ;
DELIMITER ;
# Call the stored procedure manually
call p_partition_test; # call p_partition_test();
# Open event
mysql The default is to turn off scheduled tasks
Check whether the timing task is on ,OFF It's not turned on :
show variables like '%event_scheduler%';
-- perhaps
select @@event_scheduler;
Temporarily start scheduled tasks ( After reboot , The configuration will reply )
set global event_scheduler = 1;
Permanent open , Modify the configuration file my.cnf,
event_scheduler=ON;
establish event event
delimiter $$
drop event if exists auto_pt $$
create event auto_pt
on schedule
-- every 1 minute
every 1 day
starts '2021-02-01 13:19:02'
do
BEGIN
call p_partition_test();
END$$
delimiter ;
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Cool moon, eight 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_15028305/article/details/114062707
边栏推荐
- Reprint of illustrations in nature, issue 3 - area map (part2-100)
- What is the charm of CSDN members? What's the use of him?
- Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
- Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
- Shell Scripting tips
- 【Golang】golang实现发送微信服务号模板消息
- Sentinel vs Hystrix 限流对比,到底怎么选?
- [golang] golang implements simple Memcache
- 1184. 公交站间的距离 : 简单模拟题
- Is there any charge for PDF processing? impossible!
猜你喜欢

C language programming code

Paging query of employee information of black maredge takeout
![[deserialization vulnerability-01] Introduction to serialization and deserialization](/img/e4/6b9ee6ee74f3cdc3c886ed3af9ef73.png)
[deserialization vulnerability-01] Introduction to serialization and deserialization

这才是开发者神器正确的打开方式!

Blue Bridge Cup provincial match training camp - Calculation of date

【10】团队协作和跨团队协作

[TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering

2 万字详解,吃透 ES!

Talk about software testing - automated testing framework

iMeta观点 | 短读长扩增子测序是否适用于微生物组功能的预测?
随机推荐
《Nature》论文插图复刻第3期—面积图(Part2-100)
Win10 icon turns white, recovery method
Linked list - Sword finger offer interview question 02.07. linked list intersection
Hash - 242. valid alphabetic ectopic words
2 万字详解,吃透 ES!
哈希——18. 四数之和
String - Sword finger offer 05. replace spaces
[golang] golang implements simple Memcache
Leetcode 257. all paths of binary tree
Why can't memset initialize array elements to 1?
Share the typora tool
SSH跨平台终端工具tabby推荐
这才是开发者神器正确的打开方式!
CSDN会员的魅力何在?我要他有什么用?
Types and history of bugs in it circle
Two important laws about parallelism
tcp 服务端接收数据处理思路梳理,以及select: Invalid argument报错 笔记
Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?
Performance test summary (I) -- basic theory
stream流