mysql 5.1 innodb trx_mysql 优化innodb_flush_log_at_trx_commit的案例介绍

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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值