mysql动态新建以及删除分区表_MySQL
bitsCN.com
mysql动态新建以及删除分区表
因为项目需要,最近研究了一下在mysql数据库下如何动态新建以及删除分区表。如果全部借助存储过程的话,新建以及删除分区表在逻辑上比较死板、不灵活,而且还容易出错。因此,我新建了一个数据表table_fen_qu,借助这个表可以很(相对)灵活的对分区表进行管理。以下是操作过程,不足之处请各位看官指正。
第一步:建立存储过程。
建立新建分区表的存储过程代码如下:
001
drop procedure if exists general_procedure;
002
-- general_procedure的作用:新建分区表及在table_fen_qu表中存储新建分区表时的相关参数
003
-- general_procedure的参数:表名,分区表之间的时间间隔(单位为小时),要新增的分区表个数
004
create procedure general_procedure(in tablenamein varchar(50),in intervalHour int,in newIntervalNum int)
005
general_pro:begin
006
-- 参数:最大时间
007
declare maxMonitTime datetime default SYSDATE();
008
-- 参数:最大时间对应的字符串
009
declare maxMonitTimeStr varchar(50);
010
-- 参数:最小时间
011
declare minMonitTime datetime default SYSDATE();
012
-- 参数:最大时间对应的字符串
013
declare minMonitTimeStr varchar(50);
014
-- 参数:数据库记录数
015
declare recoidNum int default 0;
016
-- 判断传入的表名是否为空
017
if tablenamein is null then
018
leave general_pro;
019
end if;
020
-- 判断传入的时间间隔
021
if intervalHour
022
set intervalHour = 6;
023
end if;
024
-- 判断新增分区表个数
025
if newIntervalNum
026
set newIntervalNum = 1;
027
end if;
028
029
-- 在该表中,查询符合条件的记录数,backupflag=0说明是未备份
030
select count(*) into recoidNum from table_fen_qu where tablename=tablenamein and backupflag=0;
031
if recoidNum > 0 then
032
-- 查询该表在table_fen_qu表中的最大监测时间
033
select monittime into maxMonitTime from table_fen_qu where tablename=tablenamein and backupflag=0 order by monittime desc limit 1;
034
-- 判断监测时间是否为null
035
if maxMonitTime is null then
036
set maxMonitTime = SYSDATE();
037
end if;
038
-- 比较最大时间减去72个小时之后的时间与系统时间的早晚
039
set recoidNum = timestampdiff(hour,SYSDATE(),DATE_SUB(maxMonitTime,INTERVAL 3 DAY));
040
-- 如果recoidNum大于0,说明最大监测时间减去72小时之后的时间仍然在系统时间之后,
041
-- 说明不用建立新的分区表,反之,则建立最大监测时间之后newIntervalNum个以每intervalHour小时为间隔的分区表
042
if recoidNum
043
set recoidNum = 1;
044
while recoidNum
045
set maxMonitTime = ADDDATE(maxMonitTime,INTERVAL intervalHour HOUR);
046
set maxMonitTimeStr = CONCAT('p',DATE_FORMAT(maxMonitTime,"%Y%m%d%H%i%s"));
047
-- 开始添加分区表
048
/*拼接分区表代码段*/
049
set @v_add_s = CONCAT('ALTER TABLE ',tablenamein,' ADD PARTITION (PARTITION ',maxMonitTimeStr,' VALUES LESS THAN (/'',maxMonitTime,'/') ENGINE = InnoDB )');
050
/*定义预处理语句*/
051
prepare stmt from @v_add_s;
052
/*执行预处理语句*/
053
execute stmt;
054
/*释放预处理语句*/
055
deallocate prepare stmt;
056
-- 开始在table_fen_qu中添加记录
057
insert into table_fen_qu(fenquname,tablename,monittime,backupflag) VALUES(maxMonitTimeStr,tablenamein,maxMonitTime,0);
058
-- 记录数加1
059
set recoidNum = recoidNum + 1;
060
end while;
061
end if;
062
else
063
set recoidNum = 1;
064
-- 计算最小时间
065
set minMonitTimeStr = CONCAT(DATE_FORMAT(DATE_SUB(maxMonitTime,INTERVAL 60 DAY),'%Y-%m-%d'),' 00:00:00');
066
set minMonitTime = STR_TO_DATE(minMonitTimeStr,'%Y-%m-%d %H:%i:%s');
067
-- 计算最大时间
068
set maxMonitTimeStr = CONCAT(DATE_FORMAT(ADDDATE(maxMonitTime,INTERVAL 4 DAY),'%Y-%m-%d'),' 00:00:00');
069
set maxMonitTime = STR_TO_DATE(maxMonitTimeStr,'%Y-%m-%d %H:%i:%s');
070
-- 计算新建表分区个数
071
set newIntervalNum = floor(timestampdiff(hour,minMonitTime,maxMonitTime) / intervalHour) + 1;
072
if newIntervalNum
073
set newIntervalNum = 10;
074
end if;
075
-- 删除所有表分区
076
set @v_del_s = CONCAT('ALTER TABLE ',tablenamein,' remove partitioning');
077
/*定义预处理语句*/
078
prepare stmt from @v_del_s;
079
/*执行预处理语句*/
080
execute stmt;
081
/*释放预处理语句*/
082
deallocate prepare stmt;
083
-- 删除所有数据
084
delete from table_fen_qu where tablename= tablenamein;
085
-- 新建分区
086
while recoidNum
087
set minMonitTimeStr = CONCAT('p',DATE_FORMAT(minMonitTime,"%Y%m%d%H%i%s"));
088
-- 开始添加分区表
089
/*拼接分区表代码段*/
090
if recoidNum = 1 then
091
set @v_add_s = CONCAT('ALTER TABLE ',tablenamein,' PARTITION BY RANGE COLUMNS(moint_time) (PARTITION ',minMonitTimeStr,' VALUES LESS THAN (/'',minMonitTime,'/') ENGINE = InnoDB )');
092
else
093
set @v_add_s = CONCAT('ALTER TABLE ',tablenamein,' ADD PARTITION (PARTITION ',minMonitTimeStr,' VALUES LESS THAN (/'',minMonitTime,'/') ENGINE = InnoDB )');
094
end if;
095
/*定义预处理语句*/
096
prepare stmt from @v_add_s;
097
/*执行预处理语句*/
098
execute stmt;
099
/*释放预处理语句*/
100
deallocate prepare stmt;
101
-- 开始在table_fen_qu中添加记录
102
insert into table_fen_qu(fenquname,tablename,monittime,backupflag) VALUES(minMonitTimeStr,tablenamein,minMonitTime,0);
103
-- 记录数加1
104
set recoidNum = recoidNum + 1;
105
set minMonitTime = ADDDATE(minMonitTime,INTERVAL intervalHour HOUR);
106
end while;
107
end if;
108
end general_pro;
删除分区表的存储过程如下:
01
drop procedure if exists del_fenqu;
02
-- 删除'temp_data','no_energy_five_minute_data','energy_five_minute_data'及'energy_five_minute_data_summarize'共4个表中已备份的分区表
03
create procedure del_fenqu()
04
del_fq:begin
05
-- 参数:记录id
06
declare myrecid int;
07
-- 参数:分区表名称
08
declare myfenquname varchar(50);
09
-- 参数:表分区所在表的表名称
10
declare mytablename varchar(50);
11
-- 参数:数据库记录数
12
declare recoidNum int default 0;
13
-- 在该表中,查询符合条件的记录数,backupflag=1说明是已备份
14
select count(*) into recoidNum from table_fen_qu where tablename in ('temp_data','no_energy_five_minute_data','energy_five_minute_data','energy_five_minute_data_summarize') and backupflag=1;
15
-- 存在符合条件的记录则全部删除
16
while recoidNum > 0 do
17
-- 查询出一条
18
select recid,fenquname,tablename into myrecid,myfenquname,mytablename from table_fen_qu where tablename in ('temp_data','no_energy_five_minute_data','energy_five_minute_data','energy_five_minute_data_summarize') and backupflag=1 order by monittime desc limit 1;
19
-- 删除数据记录
20
delete from table_fen_qu WHERE recid = myrecid;
21
-- 删除表分区
22
/*拼接分区表代码段*/
23
set @v_drop_d=CONCAT('ALTER TABLE ',mytablename,' DROP PARTITION ',myfenquname);
24
/*定义预处理语句*/
25
prepare stmt from @v_drop_d;
26
/*执行预处理语句*/
27
execute stmt;
28
/*释放预处理语句*/
29
deallocate prepare stmt;
30
-- 在该表中,查询符合条件的记录数,backupflag=1说明是已备份
31
select count(*) into recoidNum from table_fen_qu where tablename in ('temp_data','no_energy_five_minute_data','energy_five_minute_data','energy_five_minute_data_summarize') and backupflag=1;
32
end while;
33
end del_fq;
第二步:建立事件计划,定时执行事件。
事件如下:
01
-- 打开事件计划
02
SET GLOBAL event_scheduler = ON;
03
/*创建从开始时间每隔1天定时执行*/
04
drop event if exists eachDayEvent;
05
DELIMITER ||
06
create event eachDayEvent
07
on schedule every 1 day starts '2013-05-01 00:00:00'
08
on completion preserve enable
09
do
10
begin
11
-- general_procedure的参数:表名,分区表之间的时间间隔(单位为小时),要新增的分区表个数
12
-- 非能耗5分钟表-间隔6小时-6h/分区表
13
call general_procedure('no_energy_five_minute_data',6,8);
14
-- 原始数据表-间隔6小时-6h/分区表
15
call general_procedure('temp_data',6,8);
16
-- 能耗五分钟-间隔天-24h/分区表
17
call general_procedure('energy_five_minute_data',24,4);
18
-- 能耗五分钟汇总-间隔天-24h/分区表
19
call general_procedure('energy_five_minute_data_summarize',24,4);
20
-- 能耗小时表调用-间隔周-7*24h/分区表
21
call general_procedure('energy_hour_data_summarize',168,4);
22
-- 能耗分类分项5分钟表-间隔周7*24h/分区表
23
call general_procedure('energy_item_five_minute_data',168,4);
24
-- 能耗分类分项小时表-间隔季度-90*24h/分区表
25
call general_procedure('energy_item_hour_data',2160,4);
26
-- 能耗天汇总表-间隔半年-4380h/分区表
27
call general_procedure('energy_day_data_summarize',4380,4);
28
-- 删除已备份的分区表
29
call del_fenqu();
30
end ||
31
DELIMITER ;
32

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
