`
king_c
  • 浏览: 215109 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

修改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数据。

从应用的角度,批量提交也是解决问题的办法,当然要在应用场景许可的前提下。

 

分享到:
评论

相关推荐

    :innodb_flush_log_at_trx_commit 和 sync_binlog1

    这样的好处,减少了事务数据丢失的概率,而对底层硬件的 IO 要求也没有那么高(log buffer 写到文件系统中,一般只是从 log buffer 的内存转移

    fupengfei058#blog#MySQL 重要参数 innodb_flush_log_at_trx_commit 和 sy

    可以看到,只有1才能真正地保证事务的持久性,但是由于刷新操作 fsync() 是阻塞的,直到完成后才返回,我们知道写磁盘的速度是很慢的,因此 MySQL 的性能

    my.cnf参数配置实现InnoDB引擎性能优化

    在网上看了无数的my....结果是只有innodb_flush_log_at_trx_commit可以提高性能,对于1,2,3参数无论是开其中某一个,还是三个同时调节都没有影响到测试性能。我想了下,可能是我的测试数据量还不够大造成的,后续有条

    数据库优化配置.doc

    [client] port=3306 [mysql] no-beep default-character-set=utf8 [mysqld] datadir=D:/Data port=3306 server-id=...log_file_size=1G innodb_log_buffer_size=8M innodb_flush_log_at_trx_commit=2 innodb_file_per_t

    mysql参数及其优化

    query_cache_size、query_cache_type、innodb_buffer_pool_size、innodb_log_file_size、innodb_log_buffer_size、innodb_flush_logs_at_trx_commit、transaction_isolation、innodb_file_per_table、innodb_open_...

    MySQL 句柄数占用过多的解决方法

    在Windows下安装MySQL ,用了官方的配置... 后来又看到一个设置innodb_flush_log_at_trx_commit  innodb_flush_log_at_trx_commit (这个很管用)  抱怨Innodb比MyISAM慢 100倍?那么你大概是忘了调整这个值。默

    InnoDBinsert性能拐点测试

    上篇blog《InnoDBselect性能拐点测试》测试了InnoDBselect的性能拐点,...  1、调整my.cnf的参数如下:  innodb_file_per_table=0  innodb_flush_log_at_trx_commit=2  innodb_buffer_pool_size=8G  innodb_file_i

    mysql 数据库中my.ini的优化 2G内存针对站多 抗压型的设置

    默认为2402,调到512-1024最佳 innodb_additional_mem_pool_size=4M 默认为2M innodb_flush_log_at_trx_commit=1 (设置为0就是等到innodb_log_buffer_size列队满后再统一储存,默认为1) innodb_log_buffer_size=2M ...

    mysql数据库插入速度和读取速度的调整记录

    这次修改了下面四个配置项: 1)将 innodb_flush_log_at_trx_commit 配置设定为0;按过往经验设定为0,插入速度会有很大提高。 0: Write the log buffer to the log file and flush the log file every second, but ...

    mysql主从同步快速设置方法

    安装环境 centos 5.4 mysql 5.1.xx 采用rpm直接安装 xtrabackup 1.2.22 采用rpm直接安装 代码如下: [mysqld] server-id = 1 log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql ...

    centos下mysql主从同步快速设置步骤分享

    Master:/etc/my.cnf [mysqld] server-id = 1 log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql character-set-server=utf8 init_connect=’SET NAMES utf8’设定了默认字符集为utf8...

    centos下mysql主从复制设置详解

    Master:/etc/my.cnf 代码如下:[mysqld] server-id = 1log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql character-set-server=utf8 init_connect=’SET NAMES utf8′ 设定了默认...

    mysql数据库my.cnf配置文件

    # 0:如果innodb_flush_log_at_trx_commit的值为0,log buffer每秒就会被刷写日志文件到磁盘,提交事务的时候不做任何操作(执行是由mysql的master thread线程来执行的。 # 主线程中每秒会将重做日志缓冲写入磁盘的...

Global site tag (gtag.js) - Google Analytics