You need to enable JavaScript to run this app.
导航
大事务提交 binlog 优化
最近更新时间:2025.06.05 10:49:09首次发布时间:2025.06.05 10:49:09
我的收藏
有用
有用
无用
无用

本文介绍火山引擎云数据库 MySQL 版提供的大事务提交 binlog 优化功能的相关信息。

背景信息

在 MySQL 里,大事务提交时常常会让整个实例的写入操作受阻。这是因为大事务会产生很多 binlog 事件,这些事件会先存到当前会话的临时缓存中,提交时再复制到 binlog 文件里。这个过程会产生大量的输入输出操作,并且会一直给 binlog 加锁,使得其他事务没办法提交。火山引擎云数据库 MySQL 版针对这种情况做了优化,先把大事务产生的 binlog 事件写到临时文件中,提交时不用复制,直接把临时文件重命名为正式的 binlog 文件,这样大大提高了大事务提交的效率,保证不会影响其他事务的提交。

支持版本

MySQL 5.7 版本,且内核版本为 MySQL 5.7.32_20250415 及以上。

说明

您可通过查看实例信息查看实例的内核小版本。如您的实例为 MySQL 5.7.32_20250415 之前的版本,可手动升级实例内核小版本

使用说明

可以通过如下参数使用大事务提交 binlog 优化功能。

名称类型是否需要重启以生效取值范围默认值级别说明
loose_enable_binlog_large_trans_optbool[ONOFF]OFFGlobal

loose_binlog_large_trans_threshold

integer

[20971520- 18446744073709551615]

268435456(256MB)

Global

设定作为大事务进行 binlog 提交优化的事务 binlog 事件大小的阈值。事务 binlog 事件大小大于该参数的设定值时,此事务会被作为大事务进行 binlog 提交优化。该参数的单位为字节。

使用示例

  1. 在 5.7 实例上, 按如下 SQL,创建一张大表。


    create table large_table1(id bigint not null auto_increment primary key, a varchar(10000));
    
    insert into large_table1(a) select repeat('a', 10000);
    
    // 不断 insert ... select ...
    
    MySQL [test]> insert into large_table1(a) select a from large_table1;
    Query OK, 65536 rows affected (8.439 sec)
    Records: 65536  Duplicates: 0  Warnings: 0
    
    // 继续 insert ... select ...
    

  1. 确认大事务提交 binlog 优化功能为关闭状态,即确认 loose_enable_binlog_large_trans_opt 的值为 OFF后,执行下面的 SQL 并记录执行时长。

    MySQL [test]> create table tmp like large_table1;
    Query OK, 0 rows affected (0.006 sec)
    
    MySQL [test]> begin;
    Query OK, 0 rows affected (0.003 sec)
    
    MySQL [test]> insert into tmp select * from large_table1 limit 672000;
    Query OK, 672000 rows affected (2 min 0.550 sec)
    Records: 672000  Duplicates: 0  Warnings: 0
    
    MySQL [test]> commit;
    Query OK, 0 rows affected (26.278 sec)
    

    在事务提交前 tmp 表的 binlog 缓存达到 6.3GB;事务提交用时 26.278 sec,期间其他的写入事务完全被阻塞。

  2. 修改参数 loose_enable_binlog_large_trans_opt 的值为 ON,打开大事务提交 binlog 优化功能。再次执行步骤 2 的 SQL 并记录执行时长。

    MySQL [test]> create table tmp1 like large_table1;
    Query OK, 0 rows affected (0.006 sec)
    
    MySQL [test]> begin;
    Query OK, 0 rows affected (0.003 sec)
    
    MySQL [test]> insert into tmp1 select * from large_table1 limit 672000;
    Query OK, 672000 rows affected (2 min 0.514 sec)
    Records: 672000  Duplicates: 0  Warnings: 0
    
    MySQL [test]> commit;
    Query OK, 0 rows affected (0.008 sec)
    

    开启大事务提交 binlog 优化功能后,事务提交用时 0.008 sec。

参考信息

以下是在 5.7 实例上,针对不同大小事务,开启和关闭大事务提交 binlog 优化功能提交事务用时。

说明

以下数据仅供参考,不作为大事务提交 binlog 优化功能的性能数据。

事务 binlog 大小开启优化关闭优化
800MB0.138 sec2.351 sec
1.6GB0.130 sec6.749 sec
3.2GB0.097 sec12.741 sec
6.4GB0.165 sec25.508 sec