温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么理解MySQL的innodb_flush_method

发布时间:2021-11-19 12:06:06 来源:亿速云 阅读:325 作者: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
常规3个值
1、fdatasync
2、O_DSYNC
3、O_DIRECT

正常的访问方式 
用户态缓存---》内核态缓存---》磁盘

按照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为参数但是实际上是调用的系统的 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.
       他描述的是如果这样打开一个文件那么在数据从内核态缓存写到了物理磁盘前,任何试图修改文件描述符的进程都会被堵塞。如此保证了日志文件
       最大的安全性
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直接来自用户态的缓存,O_DIRECT标识对大量的
         数据写有利,因为他绕开了内核态缓存,但是他并同步METADATA(这里占时理解为INODE缓存,也就是文件的基本信息比如打开时间修改时间等)
         所以要完成同步必须同时调用O_SYNC。
         如此我们也了解为什么为什么使用O_DIRECT还会调用FSYNC()我们知道FSYNC()是全同步的,LINUX上的传统的FDATASYNC()是不同步METADATA的
         如INODE缓存,进程描述缓存。但是他能够对大量的数据绕开缓存,提高性能,需要最后同步的只是DATAFILE的METADAT。

        MYSQL有自己的缓冲,这种可以使用O_DIRECT比较好

感谢各位的阅读,以上就是“怎么理解MySQL的innodb_flush_method”的内容了,经过本文的学习后,相信大家对怎么理解MySQL的innodb_flush_method这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI