开始检查
首先,确保 ticket_history_info表是一个分区表。如果未设置分区,需要修改表结构以支持分区,将 ticket_history_info 表设置为按日期范围进行分区
alter table ticket_history_info
partition by range (to_days(call_date)) (
partition p0 values less than (to_days('2022-09-01'))
);
1.创建分区函数
-- 创建分区函数
delimiter //
create function get_partition_name(p_date date)
returns varchar(20)
deterministic
begin
declare p_month varchar(2);
declare p_year varchar(4);
set p_month = lpad(month(p_date), 2, '0');
set p_year = year(p_date);
return concat('p', p_year, p_month);
end//
delimiter ;
检查是否成功创建函数
show function status like 'get_partition_name';
2.创建存储过程,用于自动生成分区
-- 存储过程
delimiter //
create procedure create_monthly_partition()
begin
declare next_month varchar(20);
declare next_month_first_day date;
-- 计算下一个月份的名称和下个月的第一天
set next_month = concat('p', date_format(date_add(curdate(), interval 1 month), '%y%m'));
set next_month_first_day = last_day(date_add(curdate(), interval 1 month)) + interval 1 day;
-- 检查分区是否已存在
if not exists (
select null
from information_schema.partitions
where table_name = 'ticket_history_info' and table_schema = 'ccm' and partition_name = next_month
) then
-- 创建新的分区
set @sql = concat('alter table ticket_history_info add partition (partition ', next_month, ' values less than (to_days(\'', next_month_first_day, '\')))');
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
end if;
end//
delimiter ;进行测试
检查是否成功创建函数
show procedure status like 'create_monthly_partition';
执行函数
call create_monthly_partition();
查询表分区,查看是否成功分区。
select partition_name from information_schema.partitions where table_name = '表名' and table_schema = '数据库名';
可通过修改本地系统时间,来进行反复测试是否可按照月份进行分区。

返回成功,显示已创建表分区。

3.创建自动删除半年以前的表空间函数
delimiter //
create procedure delete_old_partitions()
begin
declare done int default false;
declare drop_partition_name varchar(64);
declare cur cursor for
select partition_name
from information_schema.partitions
where table_schema = '数据库名'
and table_name = 'ticket_history_info'
and partition_description < to_days(now() - interval 6 month);
declare continue handler for not found set done = true;
open cur;
read_loop: loop
fetch cur into drop_partition_name;
if done then
leave read_loop;
end if;
set @sql = concat('alter table ticket_history_info drop partition ', drop_partition_name);
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
end loop;
close cur;
-- 删除超过半年的数据
delete from ticket_history_info_copy1
where call_date < curdate() - interval 6 month;
end//
delimiter ;
4.创建调度任务,修改到每月最后一天执行执行存储过程
create event if not exists monthly_partition_event
on schedule every 1 month
starts concat(date_format(last_day(current_date), '%y-%m-'), day(last_day(current_date)), ' 23:00:00')
do
begin
call create_monthly_partition();
call delete_old_partitions();
end;
查看事件调度器是否启动
set global event_scheduler = on;
查看事件
show events;
或
show events from `数据库名`;
创建成功

5.进行测试
插入测试数据进行测试,根据call_date字段进行数据分区,
insert into ticket_history_info (call_date, src_add, calling_num, duration) values
('2024-05-24 00:00:00', 'address1', '1234567890', 60),
('2024-06-20 00:00:00', 'address2', '0987654321', 120),
('2024-05-10 00:00:00', 'address3', '1122334455', 180),
('2024-10-01 00:00:00', 'address4', '5566778899', 240);-查询每个分区的行数
select partition_name, table_rows from information_schema.partitions where table_schema = 'ccm' and table_name = 'ticket_history_info';
或者单独查询某一个分区的数据
select * from ticket_history_info partition (p202406);
到此这篇关于mysql按月自动设置表分区的实现的文章就介绍到这了,更多相关mysql 表分区内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论