修改innodb_flush_log_at_trx_commit参数提升insert性能
最近,在一个系统的慢查询日志里发现有个insert操作很慢,达到秒级,并且是比较简单的SQL语句,把语句拿出来到mysql中直接执行,速度却很快。
这种问题一般不是SQL语句本身的问题,而是在具体的应用环境中,由于并发等原因导致的。最可怀疑的地方就是在等待表级锁。
加上监控的日志来看,很多SQL是在同一时间完成的,下面的第三列是结束时间,第四列是开始时间:
14:27:30 bizId30905 1355812050 1355812045
14:27:30 bizId28907 1355812050 1355812043
14:27:30 bizId30905 1355812050 1355812047
14:27:30 bizId17388 1355812050 1355812040
14:27:30 bizId40563 1355812050 1355812044
14:27:30 bizId15477 1355812050 1355812048
14:27:30 bizId32588 1355812050 1355812048
但是通过应用的分析来看,并不存在表级锁的地方,而insert自身的操作也只是对要插入的记录本身加锁,不会影响其他并发的insert操作。
没有更好的办法,只能在MySQL写入磁盘的性能上考虑,MySQL有个innodb_flush_log_at_trx_commit参数,用来配置flush log到磁盘的时机,具体点说,是从log buffer写到log file,并写入到磁盘上的时机。这个参数的默认值是1,即每次事务提交的时候会把日志刷到磁盘,而频繁的insert操作就会引起flush log操作的不断积累,进而引发性能问题。在应用数据可接受的前提下,可以把这个值改成0,就是每秒才操作一次。修改后潜在的问题是,在事务已经提交的情况下,如果尚未写入磁盘的时候发生故障,可能丢失数据。
MySQL官网对此参数的描述如下:
If the value of innodb_flush_log_at_trx_commit is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. When the value is 1 (the default), the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2. Note that the once-per-second flushing is not 100% guaranteed to happen every second, due to process scheduling issues.
The default value of 1 is required for full ACID compliance. You can achieve better performance by setting the value different from 1, but then you can lose up to one second worth of transactions in a crash. With a value of 0, any mysqld process crash can erase the last second of transactions. With a value of 2, only an operating system crash or a power outage can erase the last second of transactions. InnoDB‘s crash recovery works regardless of the value.
其他角度的优化办法:
如果是MyISAM存储引擎,可以使用insert delay的方式来提高性能,其原理是MySQL自身会在内存中维护一个insert队列,在实际表空闲的时候insert数据。
从应用的角度,批量提交也是解决问题的办法,当然要在应用场景许可的前提下。
参考:
http://www.banping.com/2012/12/19/innodb_flush_log_at_trx_commit/
修改innodb_flush_log_at_trx_commit参数提升insert性能的更多相关文章
- 修改一行代码提升 Postgres 性能 100 倍
		
http://www.datadoghq.com/2013/08/100x-faster-postgres-performance-by-changing-1-line/ SELECT c.key, ...
 - [转载][转]修改/proc目录下的参数优化网络性能
		
原文地址:[转]修改/proc目录下的参数优化网络性能作者:雪人 网络优化 注意: 1. 参数值带有速度(rate)的参数不能在loopback接口上工作. 2.因为内核是以HZ为单位的内部时钟来定义 ...
 - HHVM 是如何提升 PHP 性能的?
		
背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...
 - 性能测试 Apache参数配置与性能调优
		
Apache性能调优 by:授客 QQ:1033553122 环境: Apache 2.4 1.选择合适的MPM(Multi -Processing Modules, 多处理模块) Unix/Linu ...
 - 有关mysql的innodb_flush_log_at_trx_commit参数【转】
		
一.参数解释 0:log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行.该模式下在事务提交的时候,不会主动触发写入磁盘的操作. 1:每次事务 ...
 - DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化
		
DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化 2017年11月29日 06:40:37 机器之心V 阅读数 2183 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
 - dotnet 启动 JIT 多核心编译提升启动性能
		
用2分钟提升十分之一的启动性能,通过在桌面程序启动 JIT 多核心编译提升启动性能 在 dotnet 可以通过让 JIT 进行多核心编译提升软件的启动性能,在默认托管的 ASP.NET 程序是开启的, ...
 - dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能
		
我对几个应用进行严格的启动性能评估,对比了在 .NET Framework 和 dotnet 6 下的应用启动性能,非常符合预期的可以看到,在用户的设备上,经过了 NGen 之后的 .NET Fram ...
 - [.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能
		
[.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能 本节导读: 上节说了缓存是以空间来换取时间的技术,介绍了客户端缓存和两种常用服务器缓布,本节主要介绍一种. ...
 
随机推荐
- 在amazon linux上安装Jenkins
			
原文请参考: https://medium.com/@itsmattburgess/installing-jenkins-on-amazon-linux-16aaa02c369c
 - SQLServer 将日期改造成标准日期格式(如: 2016/6 ->201606)
			
同事给了份Excel 数据,导到数据库之后再查出来时发现顺序不好弄.于是想从数据源中做处理. 由于数据存在,年/月 与 年/月/日 的格式不好用datetime保存,于是用varchar保存. 数据处 ...
 - ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137
			
错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...
 - git之解决冲突
			
前面几次使用git,一直对于冲突的这个问题不是很理解,感觉有些时候就会产生冲突,在此记录一下解决冲突的流程 1.git bash上面冲突显示 2.在idea上面可以看到冲突的文件 3.去解决冲突 4. ...
 - PRO*C 函数事例 2 -- 数据库操作
			
Pro*C Oracle 的嵌入式开发,数据库处理部分最好能提取到一个模块,按照对不同数据库表的操作分成不同的.pc文件(如 DbsInstStat.pc).将此模块编译成库(c文件编译时链接此库), ...
 - MyEclipse - 问题集 - Java compiler level does not match the version of the installed Java project facet
			
右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面. 在页面中的“Java”下拉列表中,选择相应 ...
 - MySQL高可用之MHA切换测试(switchover & failover)
			
Preface I've installed MasterHA yesterday,Now let's test the master-slave switch and failove ...
 - 名片管理系统demo
			
# 定义一个列表,用来储存名片 def cardInfors(): # 打印功能提示 print('欢迎使用名片管理系统v6.6.6') print('1:添加一个名片') print('2:删除一个 ...
 - K-Means和FCM聚类
			
K均值聚类是基于原型的.划分的聚类方法.聚类数K由用户指定,初始的K个聚类中心随机选取,然后将每个点分派到最近的聚类中心,形成K个簇,接下来重新计算每个簇的聚类中心,重复上一步,直到簇不发生变化或达到 ...
 - Vue折腾记 - (2)写一个不大靠谱的面包屑组件
			
先看效果图 我把页面标题和面包屑封装到一起..就不用涉及到组件的通讯了,不然又要去监听路由或者依赖状态去获取 这里写图片描述 疑惑解答: 点击父(也就是折叠菜单)为什么会跑到子菜单第一个 因为我第一个 ...