溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么理解MySQL的innodb_flush_method

發(fā)布時間:2021-11-19 12:06:06 來源:億速云 閱讀:342 作者:iii 欄目:MySQL數據庫

這篇文章主要講解了“怎么理解MySQL的innodb_flush_method”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么理解MySQL的innodb_flush_method”吧!

官方文檔描述如下:

By default, InnoDB uses the fsync()system call to flush both the data and log files. If
innodb_flush_method option is set to O_DSYNC, InnoDB uses O_SYNC to open and flush the
log files, and fsync()to flush the data files. If O_DIRECT is specified (available on some GNU/
Linux versions, FreeBSD, and Solaris), InnoDBuses O_DIRECT(or directio()on Solaris) to
open the data files, and uses fsync()to flush both the data and log files. Note that InnoDB uses
fsync() instead of fdatasync(), and it does not use O_DSYNC by default because there have
been problems with it on many varieties of Unix.

innodb_flush_method
常規(guī)3個值
1、fdatasync
2、O_DSYNC
3、O_DIRECT

正常的訪問方式 
用戶態(tài)緩存---》內核態(tài)緩存---》磁盤

按照MYSQL的描述
1、fdatasync
InnoDB uses the fsync() system call to flush both the data and log files.
Note that InnoDB uses fsync() instead of fdatasync()
雖然MYSQL可以使用fdatasync為參數但是實際上是調用的系統(tǒng)的 fsync()函數,
我們可以看看LINUX下FSYNC()函數的描述
fsync()  transfers  ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the
       file descriptor fd to the disk device (or other permanent storage device) so that all changed information can be retrieved  even
       after  the  system crashed or was rebooted.  This includes writing through or flushing a disk cache if present.  The call blocks
       until the device reports that the transfer has completed.  It also flushes metadata information associated with  the  file  (see
       stat(2)).
       簡單的說這個參數用于同步所有線程修改過的文件,而進程中的PCB中記錄了打開文件,他是通過文件描述符進行匹配的
       在LINUX內核中/usr/src/linux-headers-3.19.0-25-generic/include/linux/sched.h
       有如下的PCB結構體
       struct task_struct { }
       其中有一個files_struct的結構體,而文件描述符就是這樣一個結構體的指針
       那么只要MYSQL線程進行了刷新動作,那么他的這些文件的數據一定會同步到磁盤
2、O_DSYNC
InnoDB uses O_SYNC to open and flush the log files, and fsync()to flush the data files
當設置為這個選項的時候,當MYSQL線程打開LOGFILE的時候使用的O_SYNC的方式,而對數據文件還是使用的fsync()
我們知道對一個文件進行讀,打開是使用LINUX的
open()函數,而其中也就有這樣一個選項
O_SYNC The  file  is  opened for synchronous I/O.  Any write(2)s on the resulting file descriptor will block the calling process
       until the data has been physically written to the underlying hardware.
       他描述的是如果這樣打開一個文件那么在數據從內核態(tài)緩存寫到了物理磁盤前,任何試圖修改文件描述符的進程都會被堵塞。如此保證了日志文件
       最大的安全性
3、O_DIRECT
If O_DIRECT is specified (available on some GNU/Linux versions, FreeBSD, and Solaris), InnoDB 
uses O_DIRECT(or directio()on Solaris) to open the data files, and uses 
fsync()to flush both the data and log files.
使用這個選項MYSQL使用O_DIRECT方式打開數據文件,而fsync()會最終同步所有的數據和日志文件。
我們同樣看看open()函數中關于O_DIRECT描述
O_DIRECT (Since Linux 2.4.10)
         Try to minimize cache effects of the I/O to and from this file.  In general this will degrade performance, but it is use‐
         ful  in special situations, such as when applications do their own caching.  File I/O is done directly to/from user-space
         buffers.  The O_DIRECT flag on its own makes an effort to transfer data synchronously, but does not give  the  guarantees
         of  the  O_SYNC flag that data and necessary metadata are transferred.  To guarantee synchronous I/O, O_SYNC must be used
         in addition to O_DIRECT.  
         使用這個選項一般來說會降低性能,但是在特定的情況下比如應用程序有自己的緩存機制。I/O直接來自用戶態(tài)的緩存,O_DIRECT標識對大量的
         數據寫有利,因為他繞開了內核態(tài)緩存,但是他并同步METADATA(這里占時理解為INODE緩存,也就是文件的基本信息比如打開時間修改時間等)
         所以要完成同步必須同時調用O_SYNC。
         如此我們也了解為什么為什么使用O_DIRECT還會調用FSYNC()我們知道FSYNC()是全同步的,LINUX上的傳統(tǒng)的FDATASYNC()是不同步METADATA的
         如INODE緩存,進程描述緩存。但是他能夠對大量的數據繞開緩存,提高性能,需要最后同步的只是DATAFILE的METADAT。

        MYSQL有自己的緩沖,這種可以使用O_DIRECT比較好

感謝各位的閱讀,以上就是“怎么理解MySQL的innodb_flush_method”的內容了,經過本文的學習后,相信大家對怎么理解MySQL的innodb_flush_method這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI