天天看點

mysql innodb flush_MySQL Config--參數innodb_flush_method

延遲寫

傳統的UNIX實作在核心中設有緩沖區高速緩存或頁面高速緩存,大多數磁盤I/O都通過緩沖進行。當将資料寫入檔案時,核心通常先将該資料複制到其中一個緩沖區中,如果該緩沖區尚未寫滿,則并不将其排入輸出隊列,而是等待其寫滿或者當核心需要重用該緩沖區以便存放其他磁盤塊資料時,再将該緩沖排入輸出隊列,然後待其到達隊首時,才進行實際的I/O操作。這種輸出方式被稱為延遲寫(delayed write)(Bach [1986]第3章詳細讨論了緩沖區高速緩存)。

"延遲寫"減少了磁盤讀寫次數,但是卻降低了檔案内容的更新速度,使得欲寫到檔案中的資料在一段時間内并沒有寫到磁盤上。當系統發生故障時,這種延遲可能造成檔案更新内容的丢失。為了保證磁盤上實際檔案系統與緩沖區高速緩存中内容的一緻性,UNIX系統提供了sync、fsync和fdatasync三個函數。

sync函數隻是将所有修改過的塊緩沖區排入寫隊列,無需等待寫磁盤操作便傳回。系統守護程序(update)會周期性(30秒)調用sync函數來重新整理記憶體緩沖區。

fsync函數隻對由檔案描述符filedes指定的單一檔案起作用,需要等待寫磁盤操作結束後才傳回,確定修改過的資料被寫入到磁盤。fsync不但更新資料,還更新指定檔案中繼資料(如修改時間和通路時間)。

fdatasync函數與fsync函數類似,但fdatasync函數隻更新資料,而不更新指定檔案的屬性。

由于fsync函數除需要更新資料外,還需要更新檔案中繼資料(metadata,包括檔案大小/通路時間/修改時間等),是以fsync函數需要兩次寫,而fdatasync函數值需要一次寫。

Unfortunately fsync() will alwaysalways initialize two write operations: one for the newly written data and another one in order to update the modification time stored in the inode.

If the modification time is not a part of the transaction concept fdatasync() can be used to avoid unnecessary inode disk write operations.

參數innodb_flush_method

參數innodb_flush_method有在Unix上有6個可選值:fsync/O_DSYNC/littlesync/nosync/O_DIRECT/O_DIRECT_NO_FSYNC,由于littlesync/nosync風險較高,常用的下面4種:

fsync:預設值,使用fsync()系統調用來重新整理資料檔案和日志檔案

O_DSYNC:使用O_SYNC打開和重新整理日志檔案,使用fsync()重新整理資料檔案。(在很多UNIX版本上直接使用O_DSYNC會存在問題)

O_DIRECT:使用O_DIRECT打開資料檔案,使用fsync()來重新整理資料檔案和日志檔案。(O_DIRECT在大部分UNIX系統上可用)

O_DIRECT_NO_FSYNC:在Flush IO操作時使用O_DIRECT,但在每次write操作時跳過fsync()系統調用。(O_DIRECT_NO_FSYNC在MySQL 5.6.6版本引入,但不适合XFS和EXT4檔案系統)

生産環境中使用innodb_flush_method=O_DIRECT

The innodb_flush_method options for Unix-like systems include:

fsync: InnoDB uses the fsync() system call to flush both the data and log files. fsync is the default setting.

O_DSYNC: InnoDB uses O_SYNC to open and flush the log files, and fsync() to flush the data files. InnoDB does not use O_DSYNC directly because there have been problems with it on many varieties of Unix.

littlesync: This option is used for internal performance testing and is currently unsupported. Use at your own risk.

nosync: This option is used for internal performance testing and is currently unsupported. Use at your own risk.

O_DIRECT: InnoDB uses O_DIRECT (or directio() on Solaris) to open the data files, and uses fsync() to flush both the data and log files. This option is available on some GNU/Linux versions, FreeBSD, and Solaris.

O_DIRECT_NO_FSYNC: InnoDB uses O_DIRECT during flushing I/O, but skips the fsync() system call after each write operation.

This setting is not suitable for file systems such as XFS and EXT4, which require an fsync() system call to synchronize file system metadata changes. If you are not sure whether your file system requires an fsync() system call to synchronize file system metadata changes, use O_DIRECT instead.

On storage devices with cache, data loss is possible if data files and redo log files reside on different storage devices, and a crash occurs before data file writes are flushed from the device cache. If you use or intend to use different storage devices for redo logs and data files, use O_DIRECT instead.

如果使用帶緩存的RAID控制器,推薦使用O_DIRECT來避免作業系統和存儲系統兩次緩存。

如果使用SAN存儲來存放資料檔案和日志檔案,對于讀負載較高的存儲系統,使用fsync()或O_DSYNC可能更快。

mysql innodb flush_MySQL Config--參數innodb_flush_method

摘抄連接配接: