怎么理解MySQL的innodb

47次阅读
没有评论

共计 3330 个字符,预计需要花费 9 分钟才能阅读完成。

这篇文章主要讲解了“怎么理解 MySQL 的 innodb_flush_method”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“怎么理解 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 这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计3330字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)