当前位置: 代码网 > it编程>数据库>Mysql > MySQL 日志表改造为分区表

MySQL 日志表改造为分区表

2025年02月20日 Mysql 我要评论
前言业务有一张日志表,只需要保留 3 个月的数据,仅 3 月的数据就占用 80g 的存储空间,如果不定期清理那么磁盘容纳不下,但是每次清理的时候,使用 delete 删除非常慢,还会产生大量的 bin

前言

业务有一张日志表,只需要保留 3 个月的数据,仅 3 月的数据就占用 80g 的存储空间,如果不定期清理那么磁盘容纳不下,但是每次清理的时候,使用 delete 删除非常慢,还会产生大量的 binlog 日志,而且删除后会产生大量的空间碎片,回收需要重建表,期间还会造成临时空间增长(online ddl 排序需要使用临时空间)需要先扩磁盘,等待空间收缩后再缩容,非常麻烦。

了解到这张表几乎不会查询,只会在某种特殊情况下才会查询,所以非常适合使用分区表。所以就提出将普通表改造成分区表的方案,本文将介绍整个过程,如果业务也有相似的场景,可以作为参考。

1. 分区表改造方法

分区表改造,需要全程锁表,业务表示无法给出窗口时间,所以需要借助 onlineddl 工具,通过无锁变更的方式来改造。这里使用的工具是 gh-ost 它的原理大致如下:

官方图解 (https://github.com/github/gh-ost)

在这里插入图片描述

主要执行过程:

  • 检查是否有外键触发器及主键信息;
  • 检查是否主库或从库,是否开启 log_slave_updates 以及 binlog 信息;
  • 检查 gho 和 ghc 结尾的临时表是否存在;
  • 创建 ghc 结尾的表,存数据迁移的信息,以及 binlog 信息等;
  • 初始化 stream 的连接,添加 binlog 的监听;
  • 根据 alter 语句创建 gho 结尾的幽灵表;
  • 开启迁移数据,按照主键把源表数据写入到 gho 结尾的表上,以及 binlog apply;
  • 进入 cut-over 阶段,锁住主库的源表,等待 binlog 应用完毕,然后替换 gh-ost 表为源表;
  • 清理 ghc 表,删除 socket 文件。

cut-over 即表 rename 阶段,gh-ost 利用了 mysql 的一个特性,原子性的 rename 请求,在所有被 blocked 的请求中,rename 优先级永远是最高的。gh-ost 基于此设计了该方案:一个连接对原表加锁,另启一个连接尝试 rename 操作,此时会被阻塞住,当释放 lock 的时候,rename 会首先被执行,其他被阻塞的请求会继续应用到新表。

2. 操作步骤

下方为脱敏后的表结构,目前已有 80g 的数据,业务依赖 created_at 作为保留日期参考字段,目前有 4~8 月的数据。

create table `xxxx_log` (
  `id` bigint(20) unsigned not null auto_increment comment 'id',
  `user_id` bigint(20) not null comment '用户id',
  `user_name` varchar(60) default null comment '用户名',
  `user_ip` varchar(60) not null comment '用户ip',
  `service_ip` varchar(60) not null comment '服务端ip',
  `url` varchar(500) not null comment '访问url',
  `req_method` varchar(60) default null comment '请求类型',
  `access_time` bigint(20) default null comment '请求时间',
  `service_id` varchar(60) default null comment '服务id',
  `parameter` varchar(500) default null comment '请求参数',
  `created_at` datetime not null default current_timestamp comment '创建时间',
  `updated_at` datetime not null default current_timestamp on update current_timestamp comment '更新时间',
  `api_id` bigint(20) default null comment '资源id',
  `request_result` varchar(200) default null comment '请求结果',
  `response_param` text comment '响应出参'
  primary key (`id`),
  key `idx_user_id` (`user_id`) using btree,
  key `idx_created_at` (`created_at`) using btree,
  key `idx_service_id` (`service_id`) using btree,
  key `idx_server_id` (`server_id`) using btree
) engine=innodb default charset=utf8mb4 comment='请求日志表';

2.1 调整主键

调整主键,该操作不会锁表,不会影响用户写入,但是会造成一定负载,建议业务低峰执行:

alter table xxxx_log drop primary key, add primary key (id, created_at), algorithm=inplace, lock=none;

2.2 无锁变更

gh-ost 的使用方法参加之前的文档:

无锁变更工具使用说明:mysql gh-ost ddl 变更工具

分区表执行的 ddl 语句如下:

alter table xxxx_log
partition by range(to_days(created_at)) (
    partition p2024_01 values less than (to_days('2024-02-01')),
    partition p2024_02 values less than (to_days('2024-03-01')),
    partition p2024_03 values less than (to_days('2024-04-01')),
    partition p2024_04 values less than (to_days('2024-05-01')),
    partition p2024_05 values less than (to_days('2024-06-01')),
    partition p2024_06 values less than (to_days('2024-07-01')),
    partition p2024_07 values less than (to_days('2024-08-01')),
    partition p2024_08 values less than (to_days('2024-09-01')),
    partition p2024_09 values less than (to_days('2024-10-01')),
    partition p2024_10 values less than (to_days('2024-11-01')),   
    partition p2024_11 values less than (to_days('2024-12-01')),     
    partition p2024_12 values less than (to_days('2025-01-01')) 
);

执行完后,该表就被改造为分区表。

2.3 回滚策略

调整主键,由于 id 本身就是唯一的,所以对业务来说没有影响,不需要回滚。

调整分区表,从刚才的原理介绍可以了解到,整个过程只会增加负载,在 copy 数据到影子表的过程中,切换后还可以选择保留原表,测试无误后删除,随时可以再 rname 回去。

3. 分区表维护

3.1 创建分区

需要提前创建好分区,否则插入数据会失败,调整分区表的语句,已经创建了 2024 年整年的分区,所以到 2025 年之前,需要提前创建好 2025 年的分区,这个业务负责人和 dba 都需要注意,否则会造成故障,分区要提前创建。

-- 创建 2025 年的分区 sql 语句。
alter table xxxx_log add partition (
  partition p2025_01 values less than (to_days('2025-02-01')),
  partition p2025_02 values less than (to_days('2025-03-01')),
  partition p2025_03 values less than (to_days('2025-04-01')),
  partition p2025_04 values less than (to_days('2025-05-01')),
  partition p2025_05 values less than (to_days('2025-06-01')),
  partition p2025_06 values less than (to_days('2025-07-01')),
  partition p2025_07 values less than (to_days('2025-08-01')),
  partition p2025_08 values less than (to_days('2025-09-01')),
  partition p2025_09 values less than (to_days('2025-10-01')),
  partition p2025_10 values less than (to_days('2025-11-01')),
  partition p2025_11 values less than (to_days('2025-12-01')),
  partition p2025_12 values less than (to_days('2026-01-01'))
);

3.2 删除分区

清理数据,了解业务只需要保留 3 个月的数据,那么可以直接 drop 分区清理数据,比如清理 2024 年第一季度的数据。

alter table xxxx_log drop partition p2024_01, p2024_02, p2024_03;

3.3 分区表查询

分区表查询的方式和普通表没有差别,不过建议查询时带上分区字段,否则查询要扫描所有的分区,会比较慢。当然也可以直接选择在某个分区里面查询。

-- 在 p2024_04 查询最大和最小的 created_at
select max(created_at), min(created_at) from xxxx_log partition (p2024_04);

后记

这类日志表类型的表,需要定期清理和归档,且业务平时也不会查询,历史数据都是静态的,分区表的特性就比较友好。改造为分区表后可大幅提升可维护性。

到此这篇关于mysql 日志表改造为分区表的文章就介绍到这了,更多相关mysql 日志表改分区表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com