修改innodb_flush_log_at_trx_commit參數提升insert性能

最近,在一個系統的慢查詢日志里發現有個insert操作很慢,達到秒級,并且是比較簡單的SQL語句,把語句拿出來到mysql中直接執行,速度卻很快。

這種問題一般不是SQL語句本身的問題,而是在具體的應用環境中,由于并發等原因導致的。最可懷疑的地方就是在等待表級鎖。

加上監控的日志來看,很多SQL是在同一時間完成的,下面的第三列是結束時間,第四列是開始時間:

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

但是通過應用的分析來看,并不存在表級鎖的地方,而insert自身的操作也只是對要插入的記錄本身加鎖,不會影響其他并發的insert操作。

沒有更好的辦法,只能在MySQL寫入磁盤的性能上考慮,MySQL有個innodb_flush_log_at_trx_commit參數,用來配置flush log到磁盤的時機,具體點說,是從log buffer寫到log file,并寫入到磁盤上的時機。這個參數的默認值是1,即每次事務提交的時候會把日志刷到磁盤,而頻繁的insert操作就會引起flush log操作的不斷積累,進而引發性能問題。在應用數據可接受的前提下,可以把這個值改成0,就是每秒才操作一次。修改后潛在的問題是,在事務已經提交的情況下,如果尚未寫入磁盤的時候發生故障,可能丟失數據。

MySQL官網對此參數的描述如下:

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.

其他角度的優化辦法:

如果是MyISAM存儲引擎,可以使用insert delay的方式來提高性能,其原理是MySQL自身會在內存中維護一個insert隊列,在實際表空閑的時候insert數據。

從應用的角度,批量提交也是解決問題的辦法,當然要在應用場景許可的前提下。

?

參考:

http://www.banping.com/2012/12/19/innodb_flush_log_at_trx_commit/

轉載于:https://www.cnblogs.com/xiaotengyi/p/3568175.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391710.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391710.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391710.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode 1178. 猜字謎(位運算)

外國友人仿照中國字謎設計了一個英文版猜字謎小游戲,請你來猜猜看吧。 字謎的迷面 puzzle 按字符串形式給出,如果一個單詞 word 符合下面兩個條件,那么它就可以算作謎底: 單詞 word 中包含謎面 puzzle 的第一個字母。 單詞 word…

Nexus3.x.x上傳第三方jar

exus3.x.x上傳第三方jar: 1. create repository 選擇maven2(hosted),說明: proxy:即你可以設置代理,設置了代理之后,在你的nexus中找不到的依賴就會去配置的代理的地址中找hosted:你可以上傳你自…

責備的近義詞_考試結果危機:我們應該責備算法嗎?

責備的近義詞I’ve been considering writing on the topic of algorithms for a little while, but with the Exam Results Fiasco dominating the headline news in the UK during the past week, I felt that now is the time to look more closely into the subject.我一直…

電腦如何設置終端設置代理_如何設置一個嚴肅的Kubernetes終端

電腦如何設置終端設置代理by Chris Cooney克里斯庫尼(Chris Cooney) 如何設置一個嚴肅的Kubernetes終端 (How to set up a serious Kubernetes terminal) 所有k8s書呆子需要的CLI工具 (All the CLI tools a growing k8s nerd needs) Kubernetes comes pre-packaged with an ou…

spring cloud(二)

1. Feign應用 Feign的作用&#xff1b;使用Feign實現consumer-demo代碼中調用服務 導入啟動器依賴&#xff1b;開啟Feign功能&#xff1b;編寫Feign客戶端&#xff1b;編寫一個處理器ConsumerFeignController&#xff0c;注入Feign客戶端并使用&#xff1b;測試 <dependen…

c/c++編譯器的安裝

MinGW(Minimalist GNU For Windows)是個精簡的Windows平臺C/C、ADA及Fortran編譯器&#xff0c;相比Cygwin而言&#xff0c;體積要小很多&#xff0c;使用較為方便。 MinGW最大的特點就是編譯出來的可執行文件能夠獨立在Windows上運行。 MinGW的組成&#xff1a; 編譯器(支持C、…

滲透工具

滲透工具 https://blog.csdn.net/Fly_hps/article/details/89306104 查詢工具 https://blog.csdn.net/Fly_hps/article/details/89070552 轉載于:https://www.cnblogs.com/liuYGoo/p/11347693.html

numpy 線性代數_數據科學家的線性代數—用NumPy解釋

numpy 線性代數Machine learning and deep learning models are data-hungry. The performance of them is highly dependent on the amount of data. Thus, we tend to collect as much data as possible in order to build a robust and accurate model. Data is collected i…

spring 注解方式配置Bean

概要&#xff1a; 再classpath中掃描組件 組件掃描&#xff08;component scanning&#xff09;&#xff1a;Spring可以從classpath下自己主動掃描。偵測和實例化具有特定注解的組件特定組件包含&#xff1a; Component&#xff1a;基本注解。標示了一個受Spring管理的組件&…

主成分分析 獨立成分分析_主成分分析概述

主成分分析 獨立成分分析by Moshe Binieli由Moshe Binieli 主成分分析概述 (An overview of Principal Component Analysis) This article will explain you what Principal Component Analysis (PCA) is, why we need it and how we use it. I will try to make it as simple…

擴展方法略好于幫助方法

如果針對一個類型實例的代碼片段經常被用到&#xff0c;我們可能會想到把之封裝成幫助方法。如下是一段針對DateTime類型實例的一段代碼&#xff1a;class Program{static void Main(string[] args){DateTime d new DateTime(2001,5,18);switch (d.DayOfWeek){case DayOfWeek.…

零元學Expression Blend 4 - Chapter 25 以Text相關功能就能簡單做出具有設計感的登入畫面...

原文:零元學Expression Blend 4 - Chapter 25 以Text相關功能就能簡單做出具有設計感的登入畫面本章將交大家如何運用Blend 4 內的Text相關功能做出有設計感的登入畫面 讓你五分鐘就能快速做出一個登入畫面 ? 本章將教大家如何運用Blend 4 內的Text相關功能做出有設計感的登入…

leetcode 395. 至少有 K 個重復字符的最長子串(滑動窗口)

給你一個字符串 s 和一個整數 k &#xff0c;請你找出 s 中的最長子串&#xff0c; 要求該子串中的每一字符出現次數都不少于 k 。返回這一子串的長度。 示例 1&#xff1a; 輸入&#xff1a;s “aaabb”, k 3 輸出&#xff1a;3 解釋&#xff1a;最長子串為 “aaa” &…

冠狀病毒時代的負責任數據可視化

First, a little bit about me: I’m a data science grad student. I have been writing for Medium for a little while now. I’m a scorpio. I like long walks on beaches. And writing for Medium made me realize the importance of taking personal responsibility ove…

集合_java集合框架

轉載自http://blog.csdn.net/zsw101259/article/details/7570033 Java集合框架圖 簡化圖&#xff1a; Java平臺提供了一個全新的集合框架。“集合框架”主要由一組用來操作對象的接口組成。不同接口描述一組不同數據類型。 1、Java 2集合框架圖 ①集合接口&#xff1a;6個…

顯示隨機鍵盤

顯示隨機鍵盤 1 <!DOCTYPE html>2 <html lang"zh-cn">3 <head>4 <meta charset"utf-8">5 <title>7-77 課堂演示</title>6 <link rel"stylesheet" type"text/css" href"style…

數據特征分析-統計分析

一、統計分析 統計分析是對定量數據進行統計描述&#xff0c;常從集中趨勢和離中趨勢兩個方面分析。 集中趨勢&#xff1a;指一組數據向某一中心靠攏的傾向&#xff0c;核心在于尋找數據的代表值或中心值-統計平均數&#xff08;算數平均數和位置平均數&#xff09; 算術平均數…

心學 禪宗_禪宗宣言,用于有效的代碼審查

心學 禪宗by Jean-Charles Fabre通過讓查爾斯法布爾(Jean-Charles Fabre) 禪宗宣言&#xff0c;用于有效的代碼審查 (A zen manifesto for effective code reviews) When you are coding, interruptions really suck.當您編碼時&#xff0c;中斷確實很糟糕。 You are in the …

leetcode 896. 單調數列

如果數組是單調遞增或單調遞減的&#xff0c;那么它是單調的。 如果對于所有 i < j&#xff0c;A[i] < A[j]&#xff0c;那么數組 A 是單調遞增的。 如果對于所有 i < j&#xff0c;A[i]> A[j]&#xff0c;那么數組 A 是單調遞減的。 當給定的數組 A 是單調數組…

數據eda_銀行數據EDA:逐步

數據edaThis banking data was retrieved from Kaggle and there will be a breakdown on how the dataset will be handled from EDA (Exploratory Data Analysis) to Machine Learning algorithms.該銀行數據是從Kaggle檢索的&#xff0c;將詳細介紹如何將數據集從EDA(探索性…