|
在以前,使用 MySQL 处理大事务时,一般会出现数据库整体性能卡顿问题:单个大事务提交会引发全库 TPS 剧烈抖动,普通事务提交被大事务阻塞,磁盘 I/O 负载急剧增高,高并发业务场景下该现象尤为严重。 根源在于原生 binlog 提交流程存在固有缺陷:
GreatSQL 8.4.4-5 推出大事务 binlog 独立落盘优化特性,彻底消除原生 binlog 二次读写带来的性能损耗。
优化传统「binlog cache 溢出落盘→ binlog cache 读取 → 写入 binlog 文件」流程,预构建完整结构的 binlog cache 临时文件,事务满足阈值后直接将 cache 文件重命名为正式 binlog 文件,消除二次写 I/O,大幅缩短事务提交时间。
GreatSQL 增加了系统变量:binlog_large_commit_threshold
系统变量 binlog_large_commit_threshold 设置了 binlog 独立落盘的阈值,binlog 大小超过此值,GreatSQL 会将该事务的 binlog 独立落盘。
<code class="language-SQL" style="line-height: 1.5;">-- binlog_large_commit_threshold的默认值是128M greatsql> SHOW VARIABLES LIKE 'binlog_large_commit_threshold'; +-------------------------------+-----------+ | Variable_name | Value | +-------------------------------+-----------+ | binlog_large_commit_threshold | 134217728 | +-------------------------------+-----------+ 1 row in set (0.01 sec)
执行一个超过 128M 的大事务
<code class="language-SQL" style="line-height: 1.5;">-- 检查binlog信息,最新binlog是binlog.000003 greatsql> SHOW BINARY LOGS; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 19082048 | No | | binlog.000002 | 190815557 | No | | binlog.000003 | 158 | No | +---------------+-----------+-----------+ 3 rows in set (0.00 sec) -- 执行一个大事务 greatsql>INSERT INTO test2 SELECT * FROM test1; Query OK, 1000000 rows affected (25.41 sec) Records: 1000000 Duplicates: 0 Warnings: 0 -- 检查binglog,发现事务未写入binlog.000003,而是写入了binlog.000004 greatsql> SHOW BINARY LOGS; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 19082048 | No | | binlog.000002 | 190815557 | No | | binlog.000003 | 391 | No | | binlog.000004 | 190725193 | No | +---------------+-----------+-----------+ 4 rows in set (0.00 sec)
<code class="language-SQL" style="line-height: 1.5;">-- 设置binlog独立落盘的阈值1G greatsql> SET GLOBAL binlog_large_commit_threshold=1024*1024*1024; Query OK, 0 rows affected (0.00 sec) greatsql> SHOW BINARY LOGS; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 19082048 | No | | binlog.000002 | 190815557 | No | | binlog.000003 | 391 | No | | binlog.000004 | 190725403 | No | | binlog.000005 | 443557932 | No | +---------------+-----------+-----------+ 5 rows in set (0.00 sec) greatsql> TRUNCATE TABLE test2; Query OK, 0 rows affected (0.05 sec) -- 执行大事务,但该事务小于binlog_large_commit_threshold(1G) greatsql> SELECT now();INSERT INTO test2 SELECT * FROM test1;select now(); +---------------------+ | now() | +---------------------+ | 2026-08-13 12:18:26 | +---------------------+ 1 row in set (0.00 sec) Query OK, 1000000 rows affected (30.22 sec) Records: 1000000 Duplicates: 0 Warnings: 0 +---------------------+ | now() | +---------------------+ | 2026-08-13 12:18:56 | +---------------------+ 1 row in set (0.00 sec) -- 事务没有独立落盘,写到了binlog.000005 greatsql> SHOW BINARY LOGS; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 19082048 | No | | binlog.000002 | 190815557 | No | | binlog.000003 | 391 | No | | binlog.000004 | 190725403 | No | | binlog.000005 | 659791246 | No | +---------------+-----------+-----------+ 5 rows in set (0.00 sec)
对 sysbench 测试 TPS 的影响
2026-08-13 12:18:53 [ 33s ] thds: 10 tps: 480.99 qps: 9606.80 (r/w/o: 6725.86/1918.96/961.98) lat (ms,95%): 19.65 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:18:54 [ 34s ] thds: 10 tps: 462.02 qps: 9243.42 (r/w/o: 6470.29/1849.08/924.04) lat (ms,95%): 17.63 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:18:55 [ 35s ] thds: 10 tps: 471.95 qps: 9486.03 (r/w/o: 6648.32/1893.81/943.90) lat (ms,95%): 18.95 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:18:56 [ 36s ] thds: 10 tps: 135.01 qps: 2640.23 (r/w/o: 1840.16/530.05/270.02) lat (ms,95%): 861.95 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:18:57 [ 37s ] thds: 10 tps: 791.87 qps: 15849.30 (r/w/o: 11098.10/3167.47/1583.73) lat (ms,95%): 17.01 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:18:58 [ 38s ] thds: 10 tps: 507.26 qps: 10140.24 (r/w/o: 7094.66/2032.06/1013.52) lat (ms,95%): 17.32 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:18:59 [ 39s ] thds: 10 tps: 499.13 qps: 9991.69 (r/w/o: 7002.88/1989.53/999.27) lat (ms,95%): 16.41 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:19:00 [ 40s ] thds: 10 tps: 468.96 qps: 9385.18 (r/w/o: 6562.43/1884.84/937.92) lat (ms,95%): 17.63 err/s: 0.00 reconn/s: 0.00
在事务完成的时间点(2026-08-13 12:18:56),TPS 被明显拉低,由约 470 降低到 135,降低约 70% 。
<code class="language-SQL" style="line-height: 1.5;">-- 设置binlog独立落盘的阈值10M greatsql> SET GLOBAL binlog_large_commit_threshold=10*1024*1024; Query OK, 0 rows affected (0.00 sec) greatsql> SHOW BINARY LOGS; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 19082048 | No | | binlog.000002 | 190815557 | No | | binlog.000003 | 391 | No | | binlog.000004 | 190725403 | No | | binlog.000005 | 659791246 | No | +---------------+-----------+-----------+ 5 rows in set (0.00 sec) greatsql> TRUNCATE TABLE test2; Query OK, 0 rows affected (0.03 sec) -- 执行一个大事务(约198M) greatsql> SELECT now();INSERT INTO test2 SELECT * FROM test1;select now(); +---------------------+ | now() | +---------------------+ | 2026-08-13 12:21:53 | +---------------------+ 1 row in set (0.00 sec) Query OK, 1000000 rows affected (27.34 sec) Records: 1000000 Duplicates: 0 Warnings: 0 +---------------------+ | now() | +---------------------+ | 2026-08-13 12:22:20 | +---------------------+ 1 row in set (0.00 sec) -- 大事务没有写入binlog.000005,写到了binlog.000006 greatsql> SHOW BINARY LOGS; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 19082048 | No | | binlog.000002 | 190815557 | No | | binlog.000003 | 391 | No | | binlog.000004 | 190725403 | No | | binlog.000005 | 676207441 | No | | binlog.000006 | 208456762 | No | +---------------+-----------+-----------+ 6 rows in set (0.01 sec)
对 sysbench 测试 TPS 的影响
2026-08-13 12:22:16 [ 34s ] thds: 10 tps: 450.49 qps: 9003.74 (r/w/o: 6297.80/1804.96/900.98) lat (ms,95%): 18.61 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:17 [ 35s ] thds: 10 tps: 449.98 qps: 9022.58 (r/w/o: 6323.70/1799.92/898.96) lat (ms,95%): 17.63 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:18 [ 36s ] thds: 10 tps: 434.03 qps: 8658.63 (r/w/o: 6056.44/1733.13/869.06) lat (ms,95%): 17.32 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:19 [ 37s ] thds: 10 tps: 469.99 qps: 9391.79 (r/w/o: 6572.85/1878.96/939.98) lat (ms,95%): 18.95 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:20 [ 38s ] thds: 10 tps: 500.00 qps: 10004.97 (r/w/o: 7005.98/1999.99/999.00) lat (ms,95%): 15.55 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:21 [ 39s ] thds: 10 tps: 429.71 qps: 8572.31 (r/w/o: 5993.04/1718.85/860.42) lat (ms,95%): 31.94 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:22 [ 40s ] thds: 10 tps: 434.91 qps: 8712.29 (r/w/o: 6104.82/1738.65/868.82) lat (ms,95%): 26.68 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:23 [ 41s ] thds: 10 tps: 456.40 qps: 9096.05 (r/w/o: 6354.62/1828.62/912.81) lat (ms,95%): 23.52 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:24 [ 42s ] thds: 10 tps: 361.98 qps: 7255.68 (r/w/o: 5082.77/1447.94/724.97) lat (ms,95%): 30.81 err/s: 0.00 reconn/s: 0.00
2026-08-13 12:22:25 [ 43s ] thds: 10 tps: 425.99 qps: 8562.86 (r/w/o: 5996.91/1713.97/851.99) lat (ms,95%): 29.19 err/s: 0.00 reconn/s: 0.00
在事务完成时间点(2026-08-13 12:22:20),TPS 没有降低,大事务对 TPS 的冲击消失。
经对比测试验证,GreatSQL 大事务 binlog 独立落盘特性效果显著。该特性通过文件结构预构建与文件直接 rename 落盘的优化方式,彻底消除 binlog 二次写入带来的 IO 写放大问题,大幅减少大事务提交对全局事务的阻塞影响,有效规避了大事务提交引发的吞吐量波动,彻底消除大事务对业务整体 TPS 稳定性的冲击,确保数据库高并发 TP 业务场景的平稳运行。
合作电话:010-64087828
社区邮箱:greatsql@greatdb.com


