教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 mysql 优化innodb_flush_log_at_trx_commit的案例介绍

mysql 优化innodb_flush_log_at_trx_commit的案例介绍

发布时间:2015-10-20   编辑:jiaochengji.com
问题描述:Win7上装了一个MYSQL,需要向表中插入160多万条数据,SQL文件大概126M,发现速度奇慢。

mysql 优化innodb_flush_log_at_trx_commit的案例介绍,供大家学习参考。

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

解决办法:
找到C:\Program Files\MySQL\MySQL Server 5.5\my.ini;
修改其中一项为:innodb_flush_log_at_trx_commit=0

为什么这样修改?

1,原文是这样的:
# 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。

您可能感兴趣的文章:
mysql 优化innodb_flush_log_at_trx_commit的案例介绍
Linux下Mysql配置(rpm安装)
修改innodb_flush_log_at_trx_commit参数提升insert的性能
学习MySQL数据分页查询(limit用法)
优化mysql insert性能的方法
mysql索引管理 mysql创建索引
mysql innodb优化配置方法分享
影响MySQL性能的查询类型有哪些
mysql索引的建立原则
mysql表空间与索引查看方法

[关闭]
~ ~