MYSQL 优化innodb_flush_log_at_trx_commit

来源:互联网 发布:ubuntu g 编译命名 编辑:程序博客网 时间:2024/05/29 16:30

问题描述:我的Win7上装了一个MYSQL,今天需要向表中插入160多万条数据,SQL文件大概126M,发现速度奇慢。

解决办法:1 找到C:\Program Files\MySQL\MySQL Server 5.5\my.ini;2 令innodb_flush_log_at_trx_commit=0。

为什么这样改?

原文是这样的

# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log

# file is only flushed to disk approximately once per second.

即如果将参数innodb_flush_log_at_trx_commit设置为1,每当事务提交,InnoDB引擎就会将事务日志写入硬盘,这可以完整保证数据库的ACID特性。

如果您觉得这样的保证没有必要,并且你的事务都是比较小的事务(运行时间比较短、逻辑比较简单),你可以将innodb_flush_log_at_trx_commit设置为0或者是

2,这样可以减少访问磁盘日志的次数。0意味着日志只写入日志文件,日志文件大约每秒钟向磁盘写一次;2意味着日志在每次事务提交的时候写入日志文件,且大约每秒钟日志文件写入一次磁盘。

==============================================================================================================================

上面这个方法貌似可以解决问题,也确实可以,但是隐患很大、影响很大。数据之所以插入慢,是因为MYSQL默认情况下是自动提交的,即没插入一条记录就是一个事务,就要写一次文件,所以很慢。我们可以在一个session中把自动提交设成false(set @@autocommit=0),然后执行所有的(或者分批) insert,然后来一个commit,OK!



原创粉丝点击