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。
總結
以上是生活随笔為你收集整理的mysql 5.1 innodb trx_mysql 优化innodb_flush_log_at_trx_commit的案例介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java链接mysql输出查询_用jav
- 下一篇: python对异常_关于python中的