当前位置:网站首页>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
边栏推荐
- Learning Notes 6 - Summary of several deep learning convolutional neural networks
- MySQL对数据表已有表进行分区表的实现
- 闹钟场景识别
- 学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
- MySQL分区表实现按月份归类
- colab
- Mba-day5 Mathematics - application problems - engineering problems
- Visual Road (XII) detailed explanation of collection class
- Solution architect's small bag - 5 types of architecture diagrams
- 第六站神京门户-------手机号码的转换
猜你喜欢
随机推荐
Detailed introduction to paging exploration of MySQL index optimization
Mysql8. 0 installation guide
MySQL分区表实现按月份归类
Esp32 learning - add folder to project
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Visual common drawing (V) scatter diagram
How to Ping Baidu development board
MBA-day5數學-應用題-工程問題
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
VIM + ctags + cscope development environment construction guide
Use of SVN:
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
Mysql8.0安装指南
How to bind a process to a specified CPU
Using El popconfirm and El backtop does not take effect
VM set up static virtual machine
C语言之结构体(进阶篇)
Mysql中有关Datetime和Timestamp的使用总结
Manjaro installation and configuration (vscode, wechat, beautification, input method)









