当前位置:网站首页>MySQL partition table can be classified by month
MySQL partition table can be classified by month
2022-04-23 11:06:00 【liming89】
Source of the article : Learn through http://www.bdgxy.com/
MySQL Single table data volume , It is recommended not to exceed 2000W That's ok , Otherwise, it will have a great impact on the performance . Recently took over a project , Single table data super 7000W That's ok , A simple query statement, etc 50 No results for more than minutes , It's really uncomfortable , Final , We decided to use partition table .
Build table
General watch (innodb) After creation, there is only one idb file :
create table normal_table(id int primary key, no int)
Look at the database file :
normal_table.ibd
Create a partition table partitioned by month , Be careful ! In addition to the regular primary key , Month field ( Fields used to partition ) It must also be a primary key :
create table partition_table(id int AUTO_INCREMENT, create_date date, name varchar(10),
primary key(id, create_date)) ENGINE=INNODB DEFAULT CHARSET=utf8
partition by range(month(create_date))(
partition quarter1 values less than(4),
partition quarter2 values less than(7),
partition quarter3 values less than(10),
partition quarter4 values less than(13)
);
Look at the database file :
partition_table#p#quarter1.ibd
partition_table#p#quarter2.ibd
partition_table#p#quarter3.ibd
partition_table#p#quarter4.ibd
Insert
insert into partition_table(create_date, name) values("2021-01-25", "tom1");
insert into partition_table(create_date, name) values("2021-02-25", "tom2");
insert into partition_table(create_date, name) values("2021-03-25", "tom3");
insert into partition_table(create_date, name) values("2021-04-25", "tom4");
insert into partition_table(create_date, name) values("2021-05-25", "tom5");
insert into partition_table(create_date, name) values("2021-06-25", "tom6");
insert into partition_table(create_date, name) values("2021-07-25", "tom7");
insert into partition_table(create_date, name) values("2021-08-25", "tom8");
insert into partition_table(create_date, name) values("2021-09-25", "tom9");
insert into partition_table(create_date, name) values("2021-10-25", "tom10");
insert into partition_table(create_date, name) values("2021-11-25", "tom11");
insert into partition_table(create_date, name) values("2021-12-25", "tom12");
Inquire about
select count(*) from partition_table;
> 12
Query the second partition ( The second quarter ) The data of :
select * from partition_table PARTITION(quarter2);
4 2021-04-25 tom4
5 2021-05-25 tom5
6 2021-06-25 tom6
Delete
When you drop a table , All partition files of the table will be deleted
Add :Mysql Automatically partition by monthly table
The two stored procedures of the core :
- auto_create_partition Create table partitions for , After calling, create a table partition for the table until the end of next month .
- auto_del_partition Delete table partition for , Facilitate the recovery of historical data space .
DELIMITER $$
DROP PROCEDURE IF EXISTS auto_create_partition$$
CREATE PROCEDURE `auto_create_partition`(IN `table_name` varchar(64))
BEGIN
SET @next_month:=CONCAT(date_format(date_add(now(),interval 2 month),'%Y%m'),'01');
SET @SQL = CONCAT( 'ALTER TABLE `', table_name, '`',
' ADD PARTITION (PARTITION p', @next_month, " VALUES LESS THAN (TO_DAYS(",
@next_month ,")) );" );
PREPARE STMT FROM @SQL;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
END$$
DROP PROCEDURE IF EXISTS auto_del_partitionKaTeX parse error: Expected 'EOF', got '&' at position 362: …on_description &̲lt; TO_DAYS(CON…
DELIMITER ;
Here is an example
-- Suppose there is a table called records, Set the partition condition as end_time By month
DROP TABLE IF EXISTS `records`;
CREATE TABLE `records` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`start_time` datetime NOT NULL,
`end_time` datetime NOT NULL,
`memo` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
PRIMARY KEY (`id`,`end_time`)
)
PARTITION BY RANGE (TO_DAYS(end_time))(
PARTITION p20200801 VALUES LESS THAN ( TO_DAYS('20200801'))
);
DROP EVENT IF EXISTS records_auto_partition
;
– Create a Event, Once a month , Save up to 6 Months of data
DELIMITER C R E A T E E V E N T ‘ r e c o r d s a u t o p a r t i t i o n ‘ O N S C H E D U L E E V E R Y 1 M O N T H O N C O M P L E T I O N P R E S E R V E E N A B L E D O B E G I N c a l l a u t o c r e a t e p a r t i t i o n ( ′ r e c o r d s ′ ) ; c a l l a u t o d e l p a r t i t i o n ( ′ r e c o r d s ′ , 6 ) ; E N D CREATE EVENT `records_auto_partition` ON SCHEDULE EVERY 1 MONTH ON COMPLETION PRESERVE ENABLE DO BEGIN call auto_create_partition('records'); call auto_del_partition('records',6); END CREATEEVENT‘recordsautopartition‘ONSCHEDULEEVERY1MONTHONCOMPLETIONPRESERVEENABLEDOBEGINcallautocreatepartition(′records′);callautodelpartition(′records′,6);END
DELIMITER ;
A few notes :
- about Mysql 5.1 For the above version , The index field of a table partition must be a primary key
- Stored procedure ,DECLARE Must follow closely BEGIN, Otherwise, you will make mistakes that you can't understand
- Cursors DECLARE After defining the declaration , Otherwise, an error will be reported
- If it's self installed Mysql, There may be Event The function is not turned on , Creating Event An error will be prompted when ; modify my.cnf, stay [mysqld] Add below event_scheduler=1 Then restart it. .
This is about MySQL That's all for the article on how to classify partition tables by month , More about mysql According to the content of monthly table partition, please search rookie tutorial www.piaodoo.com Previous articles or continue to browse the relevant articles below. I hope you can support rookie tutorials in the future www.piaodoo.com!
版权声明
本文为[liming89]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102124434.html
边栏推荐
- VIM usage
- Pytorch implementation of transformer
- Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
- Mysql系列SQL查询语句书写顺序及执行顺序详解
- Manjaro installation and configuration (vscode, wechat, beautification, input method)
- Mysql中有关Datetime和Timestamp的使用总结
- Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
- Kaggle - real battle of house price prediction
- Mba-day5 Mathematics - application problems - engineering problems
- 学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
猜你喜欢
Structure of C language (Advanced)
Use of SVN:
GO接口使用
More reliable model art than deep learning
Learning Notes 6 - Summary of several deep learning convolutional neural networks
About the three commonly used auxiliary classes of JUC
Solutions to common problems in visualization (IX) background color
比深度学习更值得信赖的模型ART
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
How to quickly download vscode
随机推荐
Learning Notes 6 - Summary of several deep learning convolutional neural networks
Ueditor -- limitation of 4m size of image upload component
MySQL对数据表已有表进行分区表的实现
VScode
Mba-day5 Mathematics - application problems - engineering problems
UDP basic learning
全栈交叉编译X86完成过程经验分享
VIM usage
MySQL索引优化之分页探索详细介绍
Source insight 4.0 FAQs
MySQL8.0升级的踩坑历险记
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
Esp32 learning - use and configuration of GPIO
Mysql系列SQL查询语句书写顺序及执行顺序详解
Mysql中一千万条数据怎么快速查询
Qinglong panel pull library command update [April 20, 2022] collection is not lost
FileProvider 路径配置策略的理解
Learning website materials
学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
Use of SVN: