当前位置:网站首页>技术分享 | undo 太大了怎么办
技术分享 | undo 太大了怎么办
2022-07-06 02:38:00 【ActionTech】
作者:王雨晨
爱可生数据库工程师,负责 MySQL 日常维护及 DMP 产品支持。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
问题背景
有用户在使用 MySQL5.7 的数据库时,遇到 undo 暴涨情况,经排查存在一条慢 SQL 执行了上万秒仍没有结束,导致后续事务产生的 undo 不能清理,越来越多
在线 truncate undo log 已开启,将慢 SQL kill 掉之后,undo 大小超过 innodb_max_undo_log_size 设置的大小,但 undo 文件没有立即收缩
测试验证
测试参数如下,开启 innodb_undo_log_truncate
mysql> show variables like '%undo%';
+--------------------------+-----------+
| Variable_name | Value |
+--------------------------+-----------+
| innodb_max_undo_log_size | 104857600 |
| innodb_undo_directory | ./ |
| innodb_undo_log_truncate | ON |
| innodb_undo_logs | 128 |
| innodb_undo_tablespaces | 3 |
+--------------------------+-----------+
5 rows in set (0.00 sec)
模拟 undo 增长,超过 innodb_max_undo_log_size 设置大小
# du -sh ./undo*
152M ./undo001
296M ./undo002
15M ./undo003
查看官方文档undo清理策略,简单概括为以下:
1、启用 innodb_undo_log_truncate 后,超过 innodb_max_undo_log_size 设置大小的undo表空间被标记为截断
2、被标记的undo表空间的回滚段被设置为不活跃的,不能分配给新的事务
3、purge线程释放不需要的回滚段
4、释放回滚段后,undo表空间被截断为初始大小10M
可以看到在收缩undo大小前,需要purge线程先释放回滚段,这里涉及另一个参数 innodb_purge_rseg_truncate_frequency,默认值128,表示purge线程每调用128次,就释放回滚段一次
此次问题背景中,该参数设置的是默认值
mysql> show variables like 'innodb_purge_rseg_truncate_frequency';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| innodb_purge_rseg_truncate_frequency | 128 |
+--------------------------------------+-------+
1 row in set (0.01 sec)
所以为了尽快收缩 undo 文件,我们可以将 innodb_purge_rseg_truncate_frequency 值调小,提高 purge 线程释放回滚段的频率
//调小该值
mysql> show variables like 'innodb_purge_rseg_truncate_frequency';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| innodb_purge_rseg_truncate_frequency | 16 |
+--------------------------------------+-------+
1 row in set (0.01 sec)
//达到purge线程调用次数,释放回滚段,undo表空间被截断
# du -sh ./undo*
10M ./undo001
10M ./undo002
15M ./undo003
MySQL8.0新增 Manual Truncation
MySQL8.0 新增支持使用 SQL 语句来管理 undo 表空间
1、需要至少三个活跃的 undo 表空间,因为要保证有两个活跃的 undo 表空间来支持 Automated Truncation
手工创建一个 undo 表空间,必须以 .ibu 结尾
mysql> create undo tablespace undo_003 add datafile '/data/mysql/data/3307/undo_003.ibu';
Query OK, 0 rows affected (0.27 sec)
//三个处于 active 状态的 undo 表空间
mysql> SELECT NAME, STATE FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME LIKE '%undo%';
+-----------------+--------+
| NAME | STATE |
+-----------------+--------+
| innodb_undo_001 | active |
| innodb_undo_002 | active |
| undo_003 | active |
+-----------------+--------+
3 rows in set (0.00 sec)
2、手工截断 undo 表空间,需要先将 undo 表空间设置为 inactive
//模拟 undo 增长
# du -sh ./undo*
81M ./undo_001
157M ./undo_002
26M ./undo_003.ibu
mysql> ALTER UNDO TABLESPACE innodb_undo_002 SET INACTIVE;
Query OK, 0 rows affected (0.01 sec)
3、手工设置 inactive 后,undo 表空间被标记为截断,purge 线程会增加返回频率,快速清空并最终截断 undo 表空间,状态变为 empty
mysql> SELECT NAME, STATE FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME LIKE '%undo%';
+-----------------+--------+
| NAME | STATE |
+-----------------+--------+
| innodb_undo_001 | active |
| innodb_undo_002 | empty |
| undo_003 | active |
+-----------------+--------+
//undo 文件收缩
# du -sh ./undo*
81M ./undo_001
2.1M ./undo_002
26M ./undo_003.ibu
4、empty 状态的 undo 表空间可以重新激活使用
mysql> ALTER UNDO TABLESPACE innodb_undo_002 SET ACTIVE;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT NAME, STATE FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME LIKE '%undo%';
+-----------------+--------+
| NAME | STATE |
+-----------------+--------+
| innodb_undo_001 | active |
| innodb_undo_002 | active |
| undo_003 | active |
+-----------------+--------+
3 rows in set (0.01 sec)
5、MySQL8.0 支持删除表空间,但前提是该表空间为 empty 状态
mysql> ALTER UNDO TABLESPACE undo_003 SET INACTIVE;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT NAME, STATE FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE NAME LIKE '%undo%';
+-----------------+--------+
| NAME | STATE |
+-----------------+--------+
| innodb_undo_001 | active |
| innodb_undo_002 | active |
| undo_003 | empty |
+-----------------+--------+
3 rows in set (0.01 sec)
mysql> DROP UNDO TABLESPACE undo_003;
Query OK, 0 rows affected (0.02 sec)
mysql> SELECT TABLESPACE_NAME, FILE_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE LIKE 'UNDO LOG';
+-----------------+------------+
| TABLESPACE_NAME | FILE_NAME |
+-----------------+------------+
| innodb_undo_001 | ./undo_001 |
| innodb_undo_002 | ./undo_002 |
+-----------------+------------+
2 rows in set (0.01 sec)
边栏推荐
- 零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
- Bigder:34/100 面试感觉挺好的,没有收到录取
- DDoS attacks - are we really at war?
- Formatting occurs twice when vs code is saved
- Spherical lens and cylindrical lens
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
- 淘宝焦点图布局实战
- MySQL (IV) - transactions
- Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
- 2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
Formatting occurs twice when vs code is saved
[postgraduate entrance examination English] prepare for 2023, learn list5 words
Apt installation ZABBIX
Qt发布exe软件及修改exe应用程序图标
技术管理进阶——什么是管理者之体力、脑力、心力
RobotFramework入门(一)简要介绍及使用
从顶会论文看2022年推荐系统序列建模的趋势
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
随机推荐
SSM assembly
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
HDU_p1237_简单计算器_stack
Accident index statistics
Follow the mouse's angle and keyboard events
JS events (add, delete) and delegates
Advanced technology management - what is the physical, mental and mental strength of managers
大厂镜像库
[postgraduate entrance examination English] prepare for 2023, learn list5 words
Sword finger offer 29 Print matrix clockwise
Introduction to robotframework (II) app startup of appui automation
Bigder:34/100 面试感觉挺好的,没有收到录取
Large scale DDoS attacks take Myanmar offline
继承的构造函数
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
3D drawing ()
729. My schedule I / offer II 106 Bipartite graph
Structural theme model (I) STM package workflow
Briefly describe the implementation principle of redis cluster
从顶会论文看2022年推荐系统序列建模的趋势