修改innodb_flush_log_at_trx_commit参数提升insert性能
最近,在一個(gè)系統(tǒng)的慢查詢?nèi)罩纠锇l(fā)現(xiàn)有個(gè)insert操作很慢,達(dá)到秒級(jí),并且是比較簡(jiǎn)單的SQL語句,把語句拿出來到mysql中直接執(zhí)行,速度卻很快。
這種問題一般不是SQL語句本身的問題,而是在具體的應(yīng)用環(huán)境中,由于并發(fā)等原因?qū)е碌摹W羁蓱岩傻牡胤骄褪窃诘却砑?jí)鎖。
加上監(jiān)控的日志來看,很多SQL是在同一時(shí)間完成的,下面的第三列是結(jié)束時(shí)間,第四列是開始時(shí)間:
14:27:30 bizId30905 1355812050 1355812045 14:27:30 bizId28907 1355812050 1355812043 14:27:30 bizId30905 1355812050 1355812047 14:27:30 bizId17388 1355812050 1355812040 14:27:30 bizId40563 1355812050 1355812044 14:27:30 bizId15477 1355812050 1355812048 14:27:30 bizId32588 1355812050 1355812048但是通過應(yīng)用的分析來看,并不存在表級(jí)鎖的地方,而insert自身的操作也只是對(duì)要插入的記錄本身加鎖,不會(huì)影響其他并發(fā)的insert操作。
沒有更好的辦法,只能在MySQL寫入磁盤的性能上考慮,MySQL有個(gè)innodb_flush_log_at_trx_commit參數(shù),用來配置flush log到磁盤的時(shí)機(jī),具體點(diǎn)說,是從log buffer寫到log file,并寫入到磁盤上的時(shí)機(jī)。這個(gè)參數(shù)的默認(rèn)值是1,即每次事務(wù)提交的時(shí)候會(huì)把日志刷到磁盤,而頻繁的insert操作就會(huì)引起flush log操作的不斷積累,進(jìn)而引發(fā)性能問題。在應(yīng)用數(shù)據(jù)可接受的前提下,可以把這個(gè)值改成0,就是每秒才操作一次。修改后潛在的問題是,在事務(wù)已經(jīng)提交的情況下,如果尚未寫入磁盤的時(shí)候發(fā)生故障,可能丟失數(shù)據(jù)。
MySQL官網(wǎng)對(duì)此參數(shù)的描述如下:
If the value of?innodb_flush_log_at_trx_commit?is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. When the value is 1 (the default), the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2. Note that the once-per-second flushing is not 100% guaranteed to happen every second, due to process scheduling issues.
The default value of 1 is required for full ACID compliance. You can achieve better performance by setting the value different from 1, but then you can lose up to one second worth of transactions in a crash. With a value of 0, any?mysqld?process crash can erase the last second of transactions. With a value of 2, only an operating system crash or a power outage can erase the last second of transactions.?InnoDB‘s?crash recovery?works regardless of the value.
其他角度的優(yōu)化辦法:
如果是MyISAM存儲(chǔ)引擎,可以使用insert delay的方式來提高性能,其原理是MySQL自身會(huì)在內(nèi)存中維護(hù)一個(gè)insert隊(duì)列,在實(shí)際表空閑的時(shí)候insert數(shù)據(jù)。
從應(yīng)用的角度,批量提交也是解決問題的辦法,當(dāng)然要在應(yīng)用場(chǎng)景許可的前提下。
?
參考:
http://www.banping.com/2012/12/19/innodb_flush_log_at_trx_commit/
轉(zhuǎn)載于:https://www.cnblogs.com/xiaotengyi/p/3568175.html
總結(jié)
以上是生活随笔為你收集整理的修改innodb_flush_log_at_trx_commit参数提升insert性能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据特征分析-对比分析
- 下一篇: 梦到杀狐狸是什么意思