mysql 5.1 innodb trx_mysql 优化innodb_flush_log_at_trx_commit的案例介绍
mysql 優(yōu)化innodb_flush_log_at_trx_commit的案例介紹,供大家學(xué)習(xí)參考。
問(wèn)題描述:
Win7上裝了一個(gè)MYSQL,需要向表中插入160多萬(wàn)條數(shù)據(jù),SQL文件大概126M,發(fā)現(xiàn)速度奇慢。
解決辦法:
找到C:\Program Files\MySQL\MySQL Server 5.5\my.ini;修改其中一項(xiàng)為: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.
即如果將參數(shù)innodb_flush_log_at_trx_commit設(shè)置為1,每當(dāng)事務(wù)提交,InnoDB引擎就會(huì)將事務(wù)日志寫(xiě)入硬盤(pán),這可以完整保證數(shù)據(jù)庫(kù)的ACID特性。
如果您覺(jué)得這樣的保證沒(méi)有必要,并且你的事務(wù)都是比較小的事務(wù)(運(yùn)行時(shí)間比較短、邏輯比較簡(jiǎn)單),你可以將innodb_flush_log_at_trx_commit設(shè)置為0或者是
2,這樣可以減少訪問(wèn)磁盤(pán)日志的次數(shù)。0意味著日志只寫(xiě)入日志文件,日志文件大約每秒鐘向磁盤(pán)寫(xiě)一次;2意味著日志在每次事務(wù)提交的時(shí)候?qū)懭肴罩疚募?#xff0c;且大約每秒鐘日志文件寫(xiě)入一次磁盤(pán)。
上面這個(gè)方法貌似可以解決問(wèn)題,也確實(shí)可以,但是隱患很大、影響很大。數(shù)據(jù)之所以插入慢,是因?yàn)镸YSQL默認(rèn)情況下是自動(dòng)提交的,即沒(méi)插入一條記錄就是一個(gè)事務(wù),就要寫(xiě)一次文件,所以很慢。可以在一個(gè)session中把自動(dòng)提交設(shè)成false(set @@autocommit=0),然后執(zhí)行所有的(或者分批) insert,然后來(lái)一個(gè)commit。
總結(jié)
以上是生活随笔為你收集整理的mysql 5.1 innodb trx_mysql 优化innodb_flush_log_at_trx_commit的案例介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java链接mysql输出查询_用jav
- 下一篇: python对异常_关于python中的